【问题标题】:Golang Variadic Constructor Function? Create a Person Contact in Golang?Golang可变参数构造函数?在 Golang 中创建联系人?
【发布时间】:2018-05-21 21:11:51
【问题描述】:

我正在为学习制作联系申请。我有一个 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

}

但这确实有效....我可以要求 FirstName 和 LastName 但是我如何使所有其他字段参数可选?提前谢谢你。

【问题讨论】:

  • 您可以将其设为可变参数函数,但调用者不清楚每个参数的作用,因为它将作为单个可变参数公开。除此之外,Go 中没有“可选”参数。
  • 您的构造函数只是从参数中分配字段。为什么不只记录所需的内容而根本没有构造函数?
  • 在这里查看一些技巧:Optional Parameters?
  • 我可以拥有名字和姓氏等所需的信息,但它与联系人应用程序的灵活性有关。也许有人只有他们想快速加入的名字或姓氏和电话号码。提供选项,而不是强迫最终用户输入某些信息。

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


【解决方案1】:

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

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 } }

【讨论】:

  • 谢谢各位...感谢 Shahbazian 先生的建议。我将使用我认为的其中之一,或者至少这提供了选项。感谢您提供链接 icza。
  • 什么是 opt() 做联系人正在里面。我没有看到 opt() 内置
  • optoptions 中的一个项目,它是ContactOption 的一部分。 ContactOption 是一个接受指向 Contanct (*Contact) 的指针的函数,因此每次更改都将发生在原始实例上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多