美文网首页
Go - options模式(函数式选项模式)

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

作者: 董小贱 | 来源:发表于2021-07-03 15:56 被阅读0次

作为 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 开发人员,遇到的众多问题之一是试图将函数的参数设为可选。这是一个非常常见的用例,有一些对象应该...

  • golang中的函数选项模式

    01 介绍 在阅读 Go 语言开源项目的源码时,我们可以发现有很多使用 “函数选项模式” 的代码,“函数选项模式...

  • 五. Go(Go protobuf)

    gopath开发模式和go modules开发模式对比 goland创建项目时没用go mod模式选项的坑 在Go...

  • Maven速查手册 - 命令

    Maven命令格式 mvn [options] [] [ >] options: 命令行可选项 go...

  • JS对象的几种创建方式

    1)Object构造函数式 2)对象字面量式 3)工厂模式 4)安全工厂模式 5)构造函数模式 6)原型模式 7)...

  • go 的选项模式

    现在有个结构体如下定义: 我们需要初始化结构体,如果是其他语言,函数支持默认参数: 但是,go语言函数不支持默认参...

  • 创建对象的几种方式

    字面量式(最常用) 调用系统的Object构造函数,创建实例对象 工厂模式 构造函数模式 与工厂模式的区别: 没有...

  • 概要

    概要 IdentityServer3被打包成一个OWIN的中间件,使用经典的选项(options)模式来配置: I...

  • if-else代码优化方案①策略模式 ②Map+Function

    if-else?解决方案①策略模式 ②Map+Function函数式接口 1.策略模式解决if-else 策略模式...

  • awk

    选项 变量 模式 处理 操作 条件声明 I/O声明 函数 功能pattern scanning and proce...

网友评论

      本文标题:Go - options模式(函数式选项模式)

      本文链接:https://www.haomeiwen.com/subject/mcauultx.html