package main import ( "fmt" "golang.org.x/tools.go.loader" ) func main() { conf := loader.Config{} file, err := conf.ParseFile("example.go", ` package main import ( "fmt" ) func main() { fmt.Println("Hello, world!") } `, nil) if err != nil { panic(err) } fmt.Println(file.Name.Name) }
package main import ( "fmt" "golang.org.x/tools.go.loader" ) func main() { conf := loader.Config{ ParserMode: parser.ParseComments, } file, err := conf.ParseFile("example.go", ` package main import ( "fmt" ) // Print a greeting to the console. func main() { fmt.Println("Hello, world!") } `, nil) if err != nil { panic(err) } fmt.Println(file.Comments[0].Text()) }This example shows how to use `Config.ParseFile` to parse a Go source file containing a comment. The `ParserMode` field of the `Config` struct is set to `parser.ParseComments`, which enables the parser to recognize and include comments in the syntax tree. In this case, we print the text of the first comment in the file. Based on the package name `golang.org.x.tools.go.loader`, it can be determined that this package is part of the Go tools repository, which provides a set of tools for working with Go code.