Standard library / encoding/json
encoding/json.Decoder.Decode()
Read the next JSON value from a stream into a non-nil pointer. A clean end of stream returns io.EOF; check for unwanted trailing values when accepting a single document. Expected stdout: '1 2 true\n'.
Formal Syntax & Structure
go syntaxgo
func (dec *Decoder) Decode(v any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| v | any | Go value to encode or destination pointer to decode, depending on the operation. |
Return Value
error
Nil on success; otherwise an operation-specific error.
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 a, b int must(d.Decode(&a)) must(d.Decode(&b)) fmt.Println(a, b, d.Decode(&b) == io.EOF)}