-2

I am new to golang and trying to create json in this format using golang

{
    "Title": "You are awesome",
    "Url": "www.youareawesome.com",
    "Desc": "your awesome desc is here",
    "Payment": {
        "Discount": "15%",
        "outlets": [
            {
                "Location": "nowhere"
            },
            {
                "Location": "everywhere"
            }
        ]
    }
}

below is my code for struct

type Partner struct {
    Title   string `json:"Title"`
    URL     string `json:"Url"`
    Desc    string `json:"Desc"`
    Payment Payment `json:"Payment"`
}

type Payment struct {
    Discount string `json:"Discount"`
    outletList [] OutletItem `json:"outletList"`
} 

type OutletItem struct {
    Location string `json:"Location"`
}

this is how i am doing

partner := Partner{} 
payment := Payment{}
partner.Title = "You are awesome"
partner.Desc = "your awesome desc is here"
payment.Discount = "15%"
payment.AddOutletItem(OutletItem{Location:"nowhere"})
partner.Payment = payment
partner.Url = "www.youareawesome.com"
json.NewEncoder(w).Encode(partner)


func (payment *Payment) AddOutletItem(item OutletItem) []OutletItem {
    payment.outletList = append(payment.outletList, item)
    return payment.outletList
}

I am unable to see the outlet array in the payment object i am not sure if i have missed out any thing.

mbuechmann
  • 5,413
  • 5
  • 27
  • 40

1 Answers1

4

OutletList must be uppercase if you want to export this field

type Payment struct {
    Discount   string        `json:"Discount"`
    OutletList [] OutletItem `json:"outletList"`
}
KibGzr
  • 2,053
  • 14
  • 15