实例化结构(或对象,采用面向对象的语言)时,无论如何都不应指定私有字段的值。但是,提供可能以私有字段结尾或以完全不同的方式处理的数据是有意义的。在这种情况下,OOP保证使用构造函数,而Go约定是一种提供名为New
[YourStructure]的功能。
func NewPerson(name string) Person { return Person{name: name}}In this trivial example, the name is simply copied to the private field, but
in a more complex example, other operations could take place (e.g. checking
that the name is valid, or looking up the name and taking action depending on
the result…).



