Standard library / encoding/json

encoding/json.Decoder.Token()

Read the next token, returning delimiters as json.Delim and scalar tokens as Go values. Commas and colons are omitted; invalid nesting is rejected. Expected stdout: '[\ntrue\n]\n'.

Formal Syntax & Structure

go syntaxgo
func (dec *Decoder) Token() (Token, error)

Return Value

(Token, error)

Next token and error; io.EOF at clean end.

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "fmt"  "io"  "strings")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  d := json.NewDecoder(strings.NewReader("[true]"))  for {    t, e := d.Token()    if e == io.EOF {      break    }    must(e)    fmt.Printf("%v\n", t)  }}
Go Reference