Standard library / encoding/json

encoding/json.Decoder.Buffered()

Return a reader for data already buffered but not consumed by Decode. The reader is valid until the next Decode call. Expected stdout: '" 2"\n'.

Formal Syntax & Structure

go syntaxgo
func (dec *Decoder) Buffered() io.Reader

Return Value

io.Reader

Reader over unconsumed buffered bytes.

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("1 2"))  var n int  must(d.Decode(&n))  b, e := io.ReadAll(d.Buffered())  must(e)  fmt.Printf("%q\n", b)}
Go Reference