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
type Doc struct { | |
Map map[string]string `json:"map"` | |
} | |
func main() { | |
// source is a JSON document from an external service | |
var doc Doc | |
json.Unmarshal(source, &doc) | |
SomeFunc(doc.Map) | |
} |
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
func main() { | |
map1 := make(map[string]string) | |
map2 := make(map[string]string) | |
var mapNil map[string]string | |
mapNil = nil | |
fmt.Println("map1:", map1) | |
fmt.Println("map2:", map2) | |
fmt.Println("mapNil:", mapNil) | |
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
map1: map[] | |
map2: map[] | |
mapNil: map[] | |
Length of map1: 0 | |
Length of map2: 0 | |
Length of mapNil: 0 | |
1) map1 == map2: true | |
2) map1 == mapNil: false | |
3) map1 == mapNil || len(map1) == len(mapNil): true |
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
func main() { | |
map1 := make(map[string]string) | |
var map2 map[string]string | |
json.Unmarshal([]byte("{}"), map2) | |
var mapNil map[string]string | |
mapNil = nil | |
fmt.Println("map1:", map1) | |
fmt.Println("map2:", map2) | |
fmt.Println("mapNil:", mapNil) |
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
map1: map[] | |
map2: map[] | |
mapNil: map[] | |
Length of map1: 0 | |
Length of map2: 0 | |
Length of mapNil: 0 | |
1) map1 == map2: false | |
2) map2 == mapNil: true | |
3) map1 == mapNil || len(map1) == len(mapNil): true |
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
{ | |
"_eventId": "ad23ef1-156a-03b7-e1826fcd" | |
} |
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
{ | |
"_eventId": "ad23ef1-156a-03b7-e1826fcd", | |
"_eventid": "1234567890" | |
} |
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" | |
) | |
type eventWithBoth struct { | |
EventId string `json:"_eventId"` | |
Eventid string `json:"_eventid"` |
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
Example 1 (works as expected with both fields included on struct): | |
JSON: {"_eventId": "cased"} | |
_eventid: | |
_eventId: cased | |
Example 2 (works as expected with both fields included on struct): | |
JSON: {"_eventid": "no case"} | |
_eventid: no case | |
_eventId: |
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
// lines 559-583 in decode.go in encoding/json package | |
var f *field | |
fields := cachedTypeFields(v.Type()) | |
for i := range fields { | |
ff := &fields[i] | |
if bytes.Equal(ff.nameBytes, key) { | |
f = ff | |
break | |
} | |
if f == nil && ff.equalFold(ff.nameBytes, key) { |
OlderNewer