Instantiation of complex struct

71 views Asked by At

I created a struct in a file called availability.go

package restconsume

import (

)

// Availabilityrequest for sabre
type Availabilityrequest struct {
     OTAAirLowFareSearchRQ struct {
         OriginDestinationInformation []struct {
            DepartureDateTime   string `json:"DepartureDateTime"`
            DestinationLocation struct {
                LocationCode string `json:"LocationCode"`
            } `json:"DestinationLocation"`
            OriginLocation struct {
                LocationCode string `json:"LocationCode"`
            } `json:"OriginLocation"`
            RPH string `json:"RPH"`
        } `json:"OriginDestinationInformation"`
        POS struct {
            Source []struct {
                PseudoCityCode string `json:"PseudoCityCode" default:"F9CE"`
                RequestorID    struct {
                    CompanyName struct {
                        Code string `json:"Code" default:"TN"`
                    } `json:"CompanyName"`
                    ID   string `json:"ID" default:"1"`
                    Type string `json:"Type" default:"1"`
                } `json:"RequestorID"`
            } `json:"Source"`
        } `json:"POS"`
        TPAExtensions struct {
            IntelliSellTransaction struct {
                RequestType struct {
                    Name string `json:"Name" default:"200ITINS"`
                } `json:"RequestType"`
            } `json:"IntelliSellTransaction"`
        } `json:"TPA_Extensions"`
        TravelPreferences struct {
            TPAExtensions struct {
                DataSources struct {
                    ATPCO string `json:"ATPCO" default:"Enable"`
                    LCC   string `json:"LCC" default:"Disable"`
                    NDC   string `json:"NDC" default:"Disable"`
                } `json:"DataSources"`
                NumTrips struct {
                } `json:"NumTrips"`
            } `json:"TPA_Extensions"`
        } `json:"TravelPreferences"`
        TravelerInfoSummary struct {
            AirTravelerAvail []struct {
                PassengerTypeQuantity []struct {
                    Code     string `json:"Code"`
                    Quantity int    `json:"Quantity"`
                } `json:"PassengerTypeQuantity"`
            } `json:"AirTravelerAvail"`
            SeatsRequested []int `json:"SeatsRequested" default:"1"`
        } `json:"TravelerInfoSummary"`
        Version string `json:"Version" default:"1"`
    } `json:"OTA_AirLowFareSearchRQ"`
}

// AddADepartureDate to set the date you leave
func (a *Availabilityrequest) AddADepartureDate() Availabilityrequest {
a.OTAAirLowFareSearchRQ.OriginDestinationInformation[0].DepartureDateTime = "2020-03-21"
    return *a
}
//AddOriginDestination to set the ori and dest
func (a *Availabilityrequest) AddOriginDestination(Origin ,Destination string)  {
a.OTAAirLowFareSearchRQ.OriginDestinationInformation[0].DestinationLocation.LocationCode = Destination
    a.OTAAirLowFareSearchRQ.OriginDestinationInformation[0].OriginLocation.LocationCode = Origin
}

Now I've imported this package into my main one and having issue instatntiating with only one substruct(TPAExtensions) main.go

package main

import (
    "restconsume"
    "fmt"
)

func main() {
var a= new(restconsume.Availabilityrequest)
    a = Availabilityrequest{
          "OTA_AirLowFareSearchRQ":OTAAirLowFareSearchRQ{
        "IntelliSellTransaction": IntelliSellTransaction{
            "RequestType": RequestType{
              "Name": "200ITINS"},
          },
        },
    }
}

error message undefined: Availabilityrequest My question is how could I instantiate this kind of complex struct?

1

There are 1 answers

0
kostix On BEST ANSWER

The simplest answer is to not try to use struct literal but rather have a variable of the top-level type to be initialized to an appropriate zero value for its type and then explicitly set only those fields which are needed, like this:

var a Availabilityrequest
a.OTAAirLowFareSearchRQ.TPAExtensions.IntelliSellTransaction.RequestType.Name = "200ITINS"

But honestly, judging from your question, it looks like you're JavaScript programmer trying to attack Go without much prior knowledge about that language. This is a path to suffering.
Please be advised to at least start with the Tour of Go and then read any introductory-level book on Go (I would recommend this one).

"Effective Go" is also a must.