In some projects, we will use json to convert strings into structures, but often, this structure will only be used once, basically only for deserialization. For this structure that is only used once, we can use anonymous structures. It will be very useful when gin receives parameters, such as the json parameter we are going to receive is
{ "opt": "left", "phoneinfo": [ {"device_id": 64, "sn": "192.168.110.65:5555"}, {"device_id": 68, "sn": "192.168.110.164:5555"} ] }
When we try to use()
When a function converts a json string to a data structure in go, we can have the following ways
Convert to map
The map structure needs to define the types of key and value. For keys, it can be defined as strings, but for value, it can be seen from the above json structure that there are strings and integers, which is not easy to define. Who knows whether there will be new types in the future? So we can convert json intomap[string]interface{}
import ( "encoding/json" "fmt" ) func main() { str := `{ "opt": "left", "phoneinfo": [ {"device_id": 64, "sn": "192.168.110.65:5555"}, {"device_id": 68, "sn": "192.168.110.164:5555"} ] }` var s map[string]interface{} ([]byte(str), &s) ("%#v", s) }
This kind of conversion can be used, but if you want to use the data inside, it will be more troublesome, you need to convertinterface{}
Do type conversion If you want to get the opt parameter
opt := s["opt"].(string) (opt)
Define a named structure
Another way is to define a structure to receive parameters. If you want to receive the above json string, you can define the following structure.
package main import ( "encoding/json" "fmt" ) type phoneinfo struct { Device_id int `json:"device_id"` Sn string `json:"sn"` } type params struct { Opt string `json:"opt"` Phoneinfo []phoneinfo `json:"phoneinfo"` } func main() { str := `{ "opt": "left", "phoneinfo": [ {"device_id": 64, "sn": "192.168.110.65:5555"}, {"device_id": 68, "sn": "192.168.110.164:5555"} ] }` var s params ([]byte(str), &s) ("%#v\n", s) () //left ([0].Sn) //192.168.110.65:5555 }
Define anonymous structure
The above defines two structures, but it is obvious that these two structures are only used once here and will not be used anywhere else, so we can also use anonymous structures at this time.
func main() { str := `{ "opt": "left", "phoneinfo": [ {"device_id": 64, "sn": "192.168.110.65:5555"}, {"device_id": 68, "sn": "192.168.110.164:5555"} ] }` var s struct { Opt string `json:"opt"` Phoneinfo []struct { Device_id int `json:"device_id"` Sn string `json:"sn"` } `json:"phoneinfo"` } ([]byte(str), &s) ("%#v\n", s) () ([0].Sn) }
Directly passvar variable name struct {}
To initialize an anonymous structure variable, but if this structure needs to be used in multiple places, it is better to define a named structure separately.
This is the end of this article about the use of anonymous structures in golang. For more related content of anonymous structures in golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!