Skip to content

Instantly share code, notes, and snippets.

@dgrijalva
Created September 11, 2013 00:23
Show Gist options
  • Save dgrijalva/6517740 to your computer and use it in GitHub Desktop.
Save dgrijalva/6517740 to your computer and use it in GitHub Desktop.
An example of a subtle bug you can run into when using the JSON decoder. Try it both ways. You'll see what's what.
package main
import (
"encoding/json"
"fmt"
)
var A = []byte(`{"foo": "bar"}`)
var B = []byte(`{"bar": "baz"}`)
func main() {
// var dat map[string]interface{} // This is bad
var things = [][]byte{A,B}
for _, thing := range things {
var dat map[string]interface{} // This is good
json.Unmarshal(thing, &dat)
fmt.Println(dat)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment