Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

append a struct to a slice of struct in golang

I have this data model

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 insert the ResponseQueryHotelsData to ResponseQueryHotel.Data.Hotels right, so i try this

var output models.ResponseQueryHotel

var data models.ResponseQueryHotelsData
output.Data.Hotels = append(output.Data.Hotels, data)

but i get this error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cannot use data (variable of type models.ResponseQueryHotelsData) as struct{Data models.ResponseQueryHotelsData "json:\"data\""} value in argument to append

what should i do to a append the Data to output.Data.Hotels (there will be more than 1 ‘ResponseQueryHotelsData’ that will be inserted)

and btw i can’t change the data (it’s not up to me)

>Solution :

output.Data.Hotels = append(output.Data.Hotels, struct {
    Data ResponseQueryHotelsData `json:"data"`
}{Data: data})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading