Standard library / encoding/json

encoding/json.NewDecoder()

Construct a streaming decoder over a reader. The decoder may read beyond the requested value into its own buffer. Expected stdout: '1\n'.

Formal Syntax & Structure

go syntaxgo
func NewDecoder(r io.Reader) *Decoder

Parameters & Attributes

NameTypeDescription
rio.ReaderSource reader.

Return Value

*Decoder

New streaming JSON decoder.

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "fmt"  "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))  fmt.Println(n)}
Go Reference