Skip to content

Instantly share code, notes, and snippets.

@bernardolm
Last active November 1, 2021 20:46
Show Gist options
  • Save bernardolm/42b6253a838d03f34d65190b9b019df6 to your computer and use it in GitHub Desktop.
Save bernardolm/42b6253a838d03f34d65190b9b019df6 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
)
type amenity struct {
Category string `json:"category"`
Name string `json:"name"`
}
func main() {
strA := []byte(`{
"category": "Comida / Bebida",
"name": "Restaurante Privado"
}`)
ameniyA := amenity{}
json.Unmarshal(strA, &ameniyA)
fmt.Printf("Com valor:\n%+v\n\n", ameniyA)
// -------------------------------------------
strB := []byte(`{
"category": null,
"name": "Restaurante Privado"
}`)
ameniyB := amenity{}
json.Unmarshal(strB, &ameniyB)
fmt.Printf("Com null:\n%+v\n\n", ameniyB)
// -------------------------------------------
strC := []byte(`{
"category": undefined,
"name": "Restaurante Privado"
}`)
ameniyC := amenity{}
json.Unmarshal(strC, &ameniyC)
fmt.Printf("Com undefined:\n%+v\n\n", ameniyC)
// -------------------------------------------
strD := []byte(`{
"name": "Restaurante Privado"
}`)
ameniyD := amenity{}
json.Unmarshal(strD, &ameniyD)
fmt.Printf("Sem o field 'category':\n%+v\n\n", ameniyD)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment