Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created January 7, 2023 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/91cef92b55b59712806eb9c5e22ec1c4 to your computer and use it in GitHub Desktop.
Save podhmo/91cef92b55b59712806eb9c5e22ec1c4 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type S struct {
Name string `json:"name"`
}
func main() {
{
code := `{"name": "foo"}`
dec := json.NewDecoder(bytes.NewBufferString(code))
var ob S
if err := dec.Decode(&ob); err != nil {
panic(err)
}
fmt.Println(ob)
}
fmt.Println("----------------------------------------")
{
code := `{"name": "foo", "age": 20}`
dec := json.NewDecoder(bytes.NewBufferString(code))
dec.DisallowUnknownFields()
var ob S
if err := dec.Decode(&ob); err != nil {
panic(err)
}
fmt.Println(ob)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment