Created
September 11, 2013 00:23
-
-
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.
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 ( | |
"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