简体   繁体   中英

Default Values after Unmarshalling json in Golang

I am using json.Unmarshall in Golang to unmarshall json data into a structure. I have set all the fields in my structure to a default values before unmarshalling. Now after calling json.Unmarshall if some of the structure fields are not present in the json data,the Unmarshal should not set those fields and values of those struct fields should retain default values that I set before calling Unmashall. Is there any way to do this?

Is there any way to do this?

No, there isn't with encoding/json. You either have to write your own unmarshaling code, try to do it by providing your own UnmarshalJSON or look for a library which supports this. But honestly: Just redesign.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM