Home > other >  golang filling a string array model
golang filling a string array model

Time:02-27

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

CodePudding user response:

Use a composite literal.

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

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

  •  Tags:  
  • go
  • Related