如何初始化以下结构体?
type Sender struct { BankCode string Name string Contact struct { Name string Phone string } }
我试过了:
s := &Sender{ BankCode: "BC",Name: "NAME",Contact { Name: "NAME",Phone: "PHONE",},}
没有工作:
mixture of field:value and value initializers undefined: Contact
我试过了:
s := &Sender{ BankCode: "BC",Contact: Contact { Name: "NAME",}
没有工作:
undefined: Contact
您的联系人是一个匿名结构类型的字段.因此,您必须重复类型定义:
s := &Sender{ BankCode: "BC",Contact: struct { Name string Phone string }{ Name: "NAME",}
但是在大多数情况下,最好将一个单独的类型定义为rob74.