From f439b3e8dffb04ecd4ede0a4b32ceb9f6bec0621 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 16 May 2023 01:33:32 +0200 Subject: Print origin and scope information of closure variables Debugging and ideas for getting variables defined in this closure's scope. --- gocapturedrefrace.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 -- cgit v1.2.3