英文:
golang filling a string array model
问题
我无法弄清楚如何填充下面简单模型的数组字符串。我尝试过的方法也已经给出。如何填充数组字符串"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)
}
英文:
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)
}
答案1
得分: 1
使用复合字面量。
p1 := Person{
FirstName: "Rajeev",
LastName: "Singh",
Age: "27",
Alternate: []string{"Samantha Holder"},
}
https://play.golang.com/p/zm9Dpx3th48
英文:
Use a composite literal.
p1 := Person{
FirstName: "Rajeev",
LastName: "Singh",
Age: "27",
Alternate: []string{"Samantha Holder"},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论