Search code examples
gonaming-conventionsassertnaming

Golang guard (assert) functions naming convention


I'm wondering if there is a golang naming convention for guard (assert) functions? I've googled a bit but couldn't find anything definitive. I've read in "The Go Programming Language" book that using 'must' prefix is a common practice.

Example of the function I need:

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))
}

Solution

  • This isn't "part" of any convention, but the standard library also uses MustXX() functions, so it's a good pattern to follow, if you really need this.

    Examples:

    Exported:

    Unexported: