php Xiaobian Banana will introduce you how to attach to a slice whose element type is not a named type in golang. In Golang, we can use the built-in append function to append elements to a slice, but if the element type of the slice is not a named type, that is, an anonymous type, we need to create a temporary slice first, then append the elements to the temporary slice, and finally Then copy the elements of the temporary slice back to the original slice. Doing this ensures that the original slice's data is not lost when appending elements. Next, we will detail the specific steps of this process.
I have two structures:
type responsequeryhotel struct { querystring *string `json:"querystring"` status string `json:"status"` action string `json:"action"` refcode string `json:"refcode"` vendorid string `json:"vendorid"` searchtoken *string `json:"searchtoken"` data struct { hotels []struct { data responsequeryhotelsdata `json:"data"` } `json:"hotels"` } `json:"data"` } type responsequeryhotelsdata struct { hotelidvendor string `json:"hotelidvendor"` hotelname string `json:"hotelname"` hoteladdress string `json:"hoteladdress"` hoteldistrict string `json:"hoteldistrict"` hotelcity string `json:"hotelcity"` hotelprovince string `json:"hotelprovince"` hotelcountry string `json:"hotelcountry"` hotellat string `json:"hotellat"` hotellng string `json:"hotellng"` hotelprice string `json:"hotelprice"` hoteldiscountprice string `json:"hoteldiscountprice"` hotelphotourl string `json:"hotelphotourl"` }
Now I need to append items to responsequeryhotel.data.hotels
whose element type is not named name. So I tried this:
var output models.responsequeryhotel var data models.responsequeryhotelsdata output.data.hotels = append(output.data.hotels, data)
But I get this error:
cannot use data (variable of type models.ResponseQueryHotelsData) as struct{Data models.ResponseQueryHotelsData "json:\"data\""} value in argument to append
How should I attach items to output.data.hotels
(there will be more than 1 responsequeryhotelsdata
attached).
BTW, I can't change the structure (it's beyond my control).
If you need to use it multiple times, define an alias for the same underlying type:
type hotel = struct { data responsequeryhotelsdata `json:"data"` } hotels := []hotel{ {data: responsequeryhotelsdata{}}, {data: responsequeryhotelsdata{}}, } output.data.hotels = append(output.data.hotels, hotels...)
Or if you only need it once, declare the struct type in place:
hotels := []struct { Data ResponseQueryHotelsData `json:"data"` }{ {Data: ResponseQueryHotelsData{}}, {Data: ResponseQueryHotelsData{}}, } output.Data.Hotels = append(output.Data.Hotels, hotels...)
Assignability section of the golang specification states:
A value x of type v is assignable to a variable of type t ("x is assignable to t") if one of the following conditions is met:
The above is the detailed content of How to append to a slice whose element type is not a named type in golang. For more information, please follow other related articles on the PHP Chinese website!