douchun3680 2017-12-07 17:25
浏览 75
已采纳

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

I am making a contact application for learning. I have a 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

}

This does work however....I can require a FirstName and LastName BUT how would I make ALL the other field arguments optional? Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongliuzhuan1219 2017-12-07 17:43
    关注

    Only your use case, domain and the design can tell which approach is preferable. But you can:

    1 - Use chaining methods:

    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 - Use functional options:

    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 } }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势