Created
January 7, 2023 05:35
-
-
Save podhmo/91cef92b55b59712806eb9c5e22ec1c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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