package main import ( "fmt" "go/token" ) func main() { fileset := token.NewFileSet() file := fileset.AddFile("example.go", -1, len("package main\n")) for _, tok := range fileset.File(file.Pos(0)).LineStarts() { fmt.Println(fileset.Position(tok)) } }
package main import ( "fmt" "go/token" "go/parser" ) func main() { fileset := token.NewFileSet() file, err := parser.ParseFile(fileset, "example.go", nil, 0) if err != nil { panic(err) } for _, tok := range fileset.File(file.Pos(0)).LineStarts() { fmt.Println(fileset.Position(tok)) } }This code parses a Go source code file using the `go/parser` package and retrieves all the tokens in the file using the `go/token` package. Conclusion: In conclusion, the go/token package is used for tokenizing Go source code and breaking it down into a sequence of tokens that can be analyzed and manipulated. The examples provided show how to create a new token file object and retrieve the tokens from a Go source code file using the `NewFileSet()` and `ParseFile()` functions respectively.