golang filling a string array model

Issue

I cant figure out a way to fill the array string of the below simple model. What I have tried is also give. How do I fill the array string "Alternate"?

func main() {
    type Person struct {
        FirstName string   `json:"FirstName,omitempty"`
        LastName  string   `json:"Lastname,omitempty"`
        Age       string   `json:"Age,omitempty"`
        Alternate []string `json:"Alternate,omitempty"`
    }

    p1 := Person{FirstName: "Rajeev", LastName: "Singh", Age: "27", Alternate: ["Samantha Holder"]}

    fmt.Println(p1)
}

Solution

Use a composite literal.

p1 := Person{
    FirstName: "Rajeev",
    LastName:  "Singh",
    Age:       "27",
    Alternate: []string{"Samantha Holder"},
}

https://play.golang.com/p/zm9Dpx3th48

Answered By – The Fool

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.