package main import ( "fmt" ) func main() { input := "John Doe, 30" var s fmt.ScanState s.Init(input) // reading the name name, _, _ := s.Token(false, func(r rune) bool { return r != ',' }) fmt.Println(name) // reading the age age, _, _ := s.Token(true, func(r rune) bool { return r != ' ' }) fmt.Println(age) }
package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("data.txt") if err != nil { panic(err) } reader := bufio.NewReader(file) var s fmt.ScanState s.InitReader(reader) // reading the name name, _, _ := s.Token(false, func(r rune) bool { return r != ',' }) fmt.Println(name) // reading the age age, _, _ := s.Token(true, func(r rune) bool { return r != ' ' }) fmt.Println(age) }In this example, we open a file and create a ScanState object to read formatted data from the file. We use the Token method to read the name and age fields. Overall, go fmt ScanState is a useful tool for parsing formatted data. It is part of the "fmt" package in the Go standard library.