var txt = `{key1 = "\"value1\"\n" | key2 = { key3 = 10 } | key4 = {key5 = { key6 = value6}}}` var s scanner.Scanner s.Init(strings.NewReader(txt)) var b []byte loop: for { switch tok := s.Scan(); tok { case scanner.EOF: break loop case '|': b = append(b, ',') case '=': b = append(b, ':') case scanner.Ident: b = append(b, strconv.Quote(s.TokenText())...) default: b = append(b, s.TokenText()...) } } var m map[string]interface{} err := json.Unmarshal(b, &m) if err != nil { // handle error } fmt.Printf("%#v\n",m)
官网示例:
package main import ( "fmt" "go/scanner" "go/token" ) func main() { // src is the input that we want to tokenize. src := []byte("cos(x) + 1i*sin(x) // Euler") // Initialize the scanner. var s scanner.Scanner fset := token.NewFileSet() // positions are relative to fset file := fset.AddFile("", fset.Base(), len(src)) // register input "file" s.Init(file, src, nil /* no error handler */, scanner.ScanComments) // Repeated calls to Scan yield the token sequence found in the input. for { pos, tok, lit := s.Scan() if tok == token.EOF { break } fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit) } }