Golang 可变参数构造函数?在 Golang 中创建联系人?

标签 go constructor variadic-functions optional-parameters optional-arguments

我正在制作联系学习申请。我有一个 NewContact()。

// Contact - defines the fields of an entire Contact
type Contact struct {
    Title     string
    FirstName string
    LastName  string
    HomePhone string
    WorkPhone string
    Mobile    string
    Address   *Address
    Email     string
}    
// NewContact - Creates a new contact
func NewContact(first string, last string) *Contact {
    c := &Contact{}
    c.FirstName = first
    c.LastName = last

    return c

}

但这确实有效......我可以要求名字和姓氏,但如何使所有其他字段参数可选?预先感谢您。

最佳答案

只有您的用例、领域和设计才能判断哪种方法更可取。但你可以:

1 - 使用链接方法:

func main() {
    c := new(Contact)
    c = c.SetEmail("...").SetFirstName("...").SetLastName("...")
}

type Contact struct {
    Title     string
    FirstName string
    LastName  string
    HomePhone string
    WorkPhone string
    Mobile    string
    Address   *Address
    Email     string
}

func (c *Contact) SetTitle(v string) *Contact     { c.Title = v; return c }
func (c *Contact) SetFirstName(v string) *Contact { c.FirstName = v; return c }
func (c *Contact) SetLastName(v string) *Contact  { c.LastName = v; return c }
func (c *Contact) SetHomePhone(v string) *Contact { c.HomePhone = v; return c }
func (c *Contact) SetWorkPhone(v string) *Contact { c.WorkPhone = v; return c }
func (c *Contact) SetMobile(v string) *Contact    { c.Mobile = v; return c }
func (c *Contact) SetAddress(v *Address) *Contact { c.Address = v; return c }
func (c *Contact) SetEmail(v string) *Contact     { c.Email = v; return c }

2 - 使用功能选项:

func main() {
    c := NewContact(FirstName("..."), LastName("..."))
    _ = c
}

type Contact struct {
    Title     string
    FirstName string
    LastName  string
    HomePhone string
    WorkPhone string
    Mobile    string
    Address   *Address
    Email     string
}

func NewContact(options ...ContactOption) *Contact {
    c := new(Contact)
    for _, opt := range options {
        opt(c)
    }
    return c
}

type ContactOption func(*Contact)

func Title(v string) ContactOption        { return func(c *Contact) { c.Title = v } }
func FirstName(v string) ContactOption    { return func(c *Contact) { c.FirstName = v } }
func LastName(v string) ContactOption     { return func(c *Contact) { c.LastName = v } }
func HomePhone(v string) ContactOption    { return func(c *Contact) { c.HomePhone = v } }
func WorkPhone(v string) ContactOption    { return func(c *Contact) { c.WorkPhone = v } }
func Mobile(v string) ContactOption       { return func(c *Contact) { c.Mobile = v } }
func SetAddress(v *Address) ContactOption { return func(c *Contact) { c.Address = v } }
func Email(v string) ContactOption        { return func(c *Contact) { c.Email = v } }

关于Golang 可变参数构造函数?在 Golang 中创建联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47700530/

相关文章:

go - 如何使用“github.com/gorilla/websocket”检查websocket连接是否处于就绪状态

javascript - MouseEventConstructor 不是构造函数

javascript - Javascript constructor 属性的意义是什么?

c++ - 哎呀概念-得到意想不到的结果

macos - 使用 VSCODE 运行无法正常工作

go - 将额外数据写入 io.Writer

golang webservice意外崩溃,没有错误

javascript - ES6 rest 参数不适用于 babel

c - 使用可变宏和函数时为 "Uninitialised value was created by a stack allocation"

C:传递数组代替变量参数列表