From cd70aad6bc273a2b275f213407d0bca5d5472ae8 Mon Sep 17 00:00:00 2001 From: A1lo Date: Wed, 30 Apr 2025 18:06:33 +0800 Subject: [PATCH] load std package list to check if a package is a really "std" lib --- output.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/output.go b/output.go index 579a874..c0fbd72 100644 --- a/output.go +++ b/output.go @@ -9,9 +9,22 @@ import ( "strings" "golang.org/x/tools/go/callgraph" + "golang.org/x/tools/go/packages" "golang.org/x/tools/go/ssa" ) +var stdPackages = map[string]struct{}{} + +func init() { + pkgs, err := packages.Load(nil, "std") + if err != nil { + panic(err) + } + for _, p := range pkgs { + stdPackages[p.PkgPath] = struct{}{} + } +} + func isSynthetic(edge *callgraph.Edge) bool { // TODO: consider handling callee.Func.Pkg == nil // this could still generate a node for the call, might be useful @@ -25,10 +38,8 @@ func inStd(node *callgraph.Node) bool { } func isStdPkgPath(path string) bool { - if strings.Contains(path, ".") { - return false - } - return true + _, ok := stdPackages[path] + return ok } func printOutput(