Standard library / encoding/json
encoding/json.Unmarshal()
Decode one complete JSON value into a non-nil pointer. Unknown struct fields are ignored by default; type errors may leave partially populated data. Expected stdout: '3\n'.
Formal Syntax & Structure
go syntaxgo
func Unmarshal(data []byte, v any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| data | []byte | Input bytes to decode. |
| 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")
func must(e error) { if e != nil { panic(e) }}
func main() { var x struct { Count int `json:"count"` } must(json.Unmarshal([]byte(`{"count":3}`), &x)) fmt.Println(x.Count)}