aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2023-05-16 01:33:32 +0200
committerTeddy Wing2023-05-16 01:33:32 +0200
commitf439b3e8dffb04ecd4ede0a4b32ceb9f6bec0621 (patch)
tree8ab50cab71e2b7d9de4bb9254ed649b2dac7336d
parent3442d134b4a66ecc198da08151fdd2cfea3aad4e (diff)
downloadgocapturedrefrace-f439b3e8dffb04ecd4ede0a4b32ceb9f6bec0621.tar.bz2
Print origin and scope information of closure variables
Debugging and ideas for getting variables defined in this closure's scope.
-rw-r--r--gocapturedrefrace.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go
index d88989e..483bd7f 100644
--- a/gocapturedrefrace.go
+++ b/gocapturedrefrace.go
@@ -6,6 +6,7 @@ import (
"go/ast"
"go/printer"
"go/token"
+ "go/types"
"golang.org/x/tools/go/analysis"
)
@@ -166,6 +167,19 @@ func assignmentsInFunc(
fmt.Println("ASSIGN:", ident)
}
+ obj := pass.TypesInfo.ObjectOf(ident)
+ if obj != nil {
+ fmt.Printf("obj: %#v\n", obj)
+
+ theVar, ok := obj.(*types.Var)
+ if !ok {
+ return true
+ }
+
+ fmt.Printf("obj origin: %#v\n", theVar.Origin())
+ fmt.Printf("obj parent: %#v\n", theVar.Parent())
+ }
+
assignments = append(assignments, ident.Name)
return true