funcExample := `func add(a, b int) int { return a + b }` fset := token.NewFileSet() funcNode, err := parser.ParseFunc(fset, funcExample, nil, 0) if err != nil { log.Fatal(err) } funcPos := funcNode.Pos() fmt.Printf("Function declaration starts at %s", fset.Position(funcPos))
funcExample := `func add(a, b int) int { return a + b }` fset := token.NewFileSet() funcNode, err := parser.ParseFunc(fset, funcExample, nil, 0) if err != nil { log.Fatal(err) } funcName := funcNode.Name.Name fmt.Println("Function name:", funcName)In this example, we again parse a sample function declaration but this time we retrieve the name of the function using the `funcNode.Name.Name` approach. Package library: go/ast