package main import ( "fmt" "go/ast" "go/token" ) func main() { src := `package main; func main() { fmt.Println("Hello, World!") }` fset := token.NewFileSet() node, _ := ast.Parse(fset, src, nil, 0) pos := node.Pos() fmt.Printf("Node position: %s\n", fset.Position(pos)) }
package main import ( "fmt" "go/ast" "go/token" ) func main() { src := `package main; func main() { fmt.Println("Hello, World!") }` fset := token.NewFileSet() node, _ := ast.Parse(fset, src, nil, 0) decl := node.Decls[0] pos := decl.Pos() fmt.Printf("Declaration position: %s\n", fset.Position(pos)) }This code example demonstrates the use of the Node Pos type in the go.ast package to locate the position of a declaration node in the source code. It parses a simple Go source file and prints the position of the "func main() {...}" declaration in the file. Package library: go/ast