How to append to a slice whose element type is not a named type in golang

WBOY
Release: 2024-02-09 10:50:19
forward
1135 people have browsed it

How to append to a slice whose element type is not a named type in golang

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.

Question content

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"`
}
Copy after login

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)
Copy after login

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
Copy after login

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).

Workaround

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...)
Copy after login

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...)
Copy after login
The

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:

  • v is the same as t.
  • v and t have the same underlying type, but not type parameters, and at least one of v or t is not a named type.
  • v and t are channel types with the same element type, v is a bidirectional channel, and at least one of v or t is not a named type.
  • t is an interface type but not a type parameter, and x implements t.
  • x is the predeclared identifier nil and t is a pointer, function, slice, map, channel, or interface type, but not a type parameter.
  • x is an untyped constant that can be represented by a value of type t.

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!