【问题标题】:Golang guard (assert) functions naming conventionGolang 守卫(断言)函数命名约定
【发布时间】:2016-09-20 09:29:15
【问题描述】:

我想知道守卫(断言)函数是否有 golang 命名约定?我用谷歌搜索了一下,但找不到任何确定的东西。我在“Go 编程语言”一书中读到,使用“必须”前缀是一种常见的做法。

我需要的函数示例:

package main

func divide(a, b int) int {
    mustNotBeZero(b)
    return a / b
}

func mustNotBeZero(n int) {
    if n == 0 {
        panic("cannot divide by zero")
    }
}

func main() {
    println(divide(5, 0))
}

【问题讨论】:

  • 好吧,这个例子有点奇怪,因为它完全符合没有mustNotBeZero guard/assertion: panic 的情况。

标签: go naming-conventions assert naming


【解决方案1】:

这不是任何约定的“一部分”,但 standard library 也使用 MustXX() 函数,所以如果你真的需要,这是一个很好的模式。

例子:

导出:

未导出:

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 2013-06-22
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多