I am trying to get go to actually return me something like this:
{"map": {}}
not {"map":null}
but the encoding/json seems to detect that this is an empty map and only return the latter value.
type test struct {
MyMap map[string]string `json:"map"`
}
func main() {
testval := test{}
asjson, err := json.Marshal(testval)
fmt.Println(testval)
fmt.Println(string(asjson))
}
The output is like this
{map[]}
{"map":null}
I am looking to get it to be {"map":{}}
suggestions? I have tried to initialize the map manually, and use a reference for it. Neither seems to yield the output I want. :/
test.MyMap
hasn't been initialized, so it is nil
. Initializing it will give you the desired result:
testval := test{
MyMap: make(map[string]string),
}
https://play.golang.org/p/91vZtJeot3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With