Go - options模式(函数式选项模式)


本文摘自网络,作者,侵删。

作为 Golang 开发人员,遇到的众多问题之一是试图将函数的参数设为可选。这是一个非常常见的用例,有一些对象应该使用一些基本的默认设置开箱即用,并且您可能偶尔想要提供一些更详细的配置。

在python 中,你可以给参数一个默认值,并在调用方法时省略它们。但是在 Golang 中,是无法这么做。

那么怎么解决这个问题呢? 答案就是Options模式。Options模式在Golang中应用十分广泛,几乎每个框架中都有他的踪迹。

  1. 传统方式:
package main

import "fmt"

type Person struct {
    Name string
    Age int
    Gender int
    Height int

    Country string
    Address string
}


func NewPerson(name string, age, gender, height int, country, address string) *Person{
    return &Person{
        Name: name,
        Age:age,
        Gender:gender,
        Height:height,
        Country:country,
        Address:address,
    }
}

func main()  {
    person := NewPerson("dongxiaojian", 18, 1, 180, "china", "Beijing")
    fmt.Printf("%+v\n", person)
}

可以发现,在这种方式中,拓展起来是非常麻烦的,以及设置出默认值十分繁琐。

  1. options模式

    package main
    
    import "fmt"
    
    type Person struct {
     Name string
     Age int
     Gender int
     Height int
    
     Country string
     City    string
    }
    
    type Options func(*Person)
    
    func WithPersonProperty (name string, age,gender,height int) Options {
     return func(p *Person){
         p.Name = name
         p.Age = age
         p.Gender = gender
         p.Height = height
     }
    }
    
    func WithRegional(country, city string) Options {
     return func(p *Person){
         p.Country = country
         p.City = city
     }
    }
    
    func NewPerson(opt ...Options) *Person{
     p := new(Person)
     p.Country = "china"
     p.City = "beijing"
     for _, o := range opt{
         o(p)
     }
     return p
    }
    
    func main()  {
    
     // 默认值方式
     person := NewPerson(WithPersonProperty("dongxiaojian", 18, 1, 180))
     fmt.Printf("%+v\n", person)
    
     // 设置值
     person2 := NewPerson(WithPersonProperty("dongxiaojian", 18, 1, 180),WithRegional("china", "hebei"))
     fmt.Printf("%+v\n", person2)
    }
    
    
    

    下次可以通过with**函数进行增加属性,以及使用默认值。 这样看起来条理清晰了很多。


本文来自:简书

感谢作者:董小贱

查看原文:Go - options模式(函数式选项模式)

相关阅读 >>

Golang能封装成dll吗

Golang sleep为什么没有返回值

Golang阿里云api请求鉴权

Go属于解释型语言么

单核cpu下Golang调度及抢占式调度的实现

Golang判断错误类型的方法

Go属于动态语言么

Go实战仿百度云盘 实现企业级分布式云存储系统

Goalng 结构体比较问题

Golang 管道线程安全吗

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...