【问题标题】:Default value in Go's methodGo 方法中的默认值
【发布时间】:2013-11-05 21:44:58
【问题描述】:

有没有办法在 Go 的函数中指定默认值?我试图在文档中找到它,但我找不到任何说明这是可能的。

func SaySomething(i string = "Hello")(string){
...
}

【问题讨论】:

    标签: go default-value function-call default-arguments


    【解决方案1】:

    否,但还有一些其他选项可以实现默认值。关于这个主题有一些good blog posts,但这里有一些具体的例子。


    **选项 1:** 调用者选择使用默认值
    // Both parameters are optional, use empty string for default value
    func Concat1(a string, b int) string {
      if a == "" {
        a = "default-a"
      }
      if b == 0 {
        b = 5
      }
    
      return fmt.Sprintf("%s%d", a, b)
    }
    

    **选项2:**最后一个可选参数
    // a is required, b is optional.
    // Only the first value in b_optional will be used.
    func Concat2(a string, b_optional ...int) string {
      b := 5
      if len(b_optional) > 0 {
        b = b_optional[0]
      }
    
      return fmt.Sprintf("%s%d", a, b)
    }
    

    **选项 3:** 配置结构
    // A declarative default value syntax
    // Empty values will be replaced with defaults
    type Parameters struct {
      A string `default:"default-a"` // this only works with strings
      B string // default is 5
    }
    
    func Concat3(prm Parameters) string {
      typ := reflect.TypeOf(prm)
    
      if prm.A == "" {
        f, _ := typ.FieldByName("A")
        prm.A = f.Tag.Get("default")
      }
    
      if prm.B == 0 {
        prm.B = 5
      }
    
      return fmt.Sprintf("%s%d", prm.A, prm.B)
    }
    

    **选项 4:** 完整的可变参数解析(javascript 样式)
    func Concat4(args ...interface{}) string {
      a := "default-a"
      b := 5
    
      for _, arg := range args {
        switch t := arg.(type) {
          case string:
            a = t
          case int:
            b = t
          default:
            panic("Unknown argument")
        }
      }
    
      return fmt.Sprintf("%s%d", a, b)
    }
    

    【讨论】:

    • 多么痛苦。我希望它是:func Concat1(a string = 'foo', b int = 10) string {,就像在大多数其他现代语言中一样...它将任何给定的示例几乎都减少到一行代码。
    • 我们是否可以选择编写两个不同的函数,并让调用者明确他们的期望。
    • @ProgramCpp 不,golang 不允许函数重载Does the Go language have function/method overloading?
    【解决方案2】:
    【解决方案3】:

    不,无法指定默认值。我相信这样做是为了提高可读性,但代价是作者付出了更多的时间(希望是思考)。

    我认为拥有“默认值”的正确方法是拥有一个新函数,该函数将默认值提供给更通用的函数。有了这个,你的代码就更清楚你的意图了。例如:

    func SaySomething(say string) {
        // All the complicated bits involved in saying something
    }
    
    func SayHello() {
        SaySomething("Hello")
    }
    

    不费吹灰之力,我制作了一个做普通事情的函数,并重用了通用函数。您可以在许多库中看到这一点,例如 fmt.Println 只需在 fmt.Print 否则会执行的操作中添加一个换行符。然而,在阅读某人的代码时,他们调用的函数很清楚他们打算做什么。使用默认值,如果不去函数引用默认值实际是什么,我将不知道应该发生什么。

    【讨论】:

    • 我喜欢并同意这个答案,但该死的,我仍然希望他们至少有一种迟钝但惯用的做法。
    • "just add another function" 参数可能会导致处理多个排列所需的方法激增,需要为这些方法,并增加了学习使用更多方法而不是更少方法的包的负担。 #jmtcw
    • 好吧,作为一个在 10 到 20 年后不得不解开堆积如山的源代码以找到错误以支持客户的人,我认为额外的函数名称可能会有一些好处
    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2021-11-18
    • 2017-08-24
    相关资源
    最近更新 更多