追加到struct中的struct数组

时间:2017-01-25 04:22:57

标签: arrays go struct

type Customer struct{
    UID string     
    Name string 
    Contact []ContactInfo
}
type ContactInfo struct {
    Number int
}

可以有多个客户,每个客户可以拥有多个联系号码。 我使用以下方法为特定用户追加struct的Contact数组,如下所示。

customer := &Customer{}
customer.UID = args[0]
customer.Name = args[1]
c:= &ContactInfo{}
c.Number = args[2]
customer.Contact= append(customer.Contact, *c)

但是这种方法不起作用,因为我只获得最新的联系人而不是输出中的联系人数组。

1 个答案:

答案 0 :(得分:0)

当您将对象按值传递给函数或方法时,可以观察到行为。这些功能内部的修改在外面不可见。

请参阅以下代码

23:41
19:07
-19:-19 //invalid numbers
相关问题