I don't know. Here is what ChatGPT 4o mini suggested, accessed at https://chatgpt.com/; with some minor corrections to make it build.
It seems to suggest there is no shortcut crosslink. The code below does demonstrate how to use the more up-to-date "golang.org/x/tools/go/packages" which handles modern modules (versus the older "golang.org/x/tools/go/loader ", pointed to/demonstrated above, which does not). Q: In golang, How do you make the jump from a go/type.TypeName to ast.Decl ? Answer: In Go, if you want to convert a type or a type name (go/types.TypeName) to an ast.Decl, you're essentially looking to map a type information structure from the go/types package to its corresponding declaration in the go/token package. The go/types.TypeName represents a type name in the Go type system, while ast.Decl is part of the abstract syntax tree (AST) and represents a declaration in Go source code. Here's a general approach you can take to achieve this: 1. *Locate the Source Position:* Use the go/types package to get the position of the type declaration. The go/types.TypeName object should have information about its position in the source code, but this might be a bit indirect since go/types does not provide direct mapping to ast.Decl. 2. *Read the Source File:* Once you have the position, you can use the go/parser package to read and parse the source file to find the specific declaration. 3. *Map the Position to the AST Declaration:* Using the source file content and the position, you can find the corresponding declaration in the AST. Here’s a step-by-step outline of how you might implement this: Example Code package main import ( "fmt" //"go/parser" "go/token" //"go/types" "log" "golang.org/x/tools/go/packages" ) func findDecl(pkgPath string, typeName string) (*token.Position, string) { // Load the package cfg := &packages.Config{Mode: packages.NeedTypes | packages.NeedFiles | packages.NeedSyntax} pkgs, err := packages.Load(cfg, pkgPath) if err != nil { log.Fatalf("failed to load package: %v", err) } // Find the type name var typePos token.Position for _, pkg := range pkgs { for _, file := range pkg.Syntax { for _, decl := range file.Decls { if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.TYPE { for _, spec := range genDecl.Specs { if typeSpec, ok := spec.(*ast.TypeSpec); ok && typeSpec.Name.Name == typeName { // Found the type declaration typePos = pkg.Fset.Position(typeSpec.Pos()) return &typePos, pkg.Fset.File(typeSpec.Pos()).Name() } } } } } } return nil, "" } func main() { // Example usage pos, file := findDecl("your_package_path", "YourTypeName") if pos != nil { fmt.Printf("Type declaration found in file %s at position %v\n", file, *pos) } else { fmt.Println("Type declaration not found") } } Explanation 1. *Load Package:* Use golang.org/x/tools/go/packages to load the Go package you’re interested in. This will give you access to the syntax trees and type information. 2. *Search for Type Declaration:* Iterate through the syntax trees to find the type declaration that matches your typeName. This is done by checking each declaration in the file to see if it matches the type you're looking for. 3. *Get Position:* Once you find the type declaration, get its position using pkg.Fset.Position(). This gives you the line and column information of the declaration. 4. *Return Results:* Return the position and filename where the type declaration was found. Notes - This approach assumes that you have access to the source code files and that you can load them using the packages package. - The go/types package itself does not directly provide a way to get an ast.Decl, so this indirect approach through positions and source file parsing is necessary. - Make sure to adjust pkgPath and YourTypeName according to your specific use case. This method is useful for tooling and analysis where you need to connect type information with its corresponding code in the source file. end Answer. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/90942574-2f3b-4e15-912f-897fae3bb69bn%40googlegroups.com.