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))
}
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:
func MustCompile(str string) *Regexp
func Must(t *Template, err error) *Template
src/syscall/dll_windows.go
: (on Windows)
func MustLoadDLL(name string) *DLL
func (d *DLL) MustFindProc(name string) *Proc
Unexported:
src/cmd/go/go_test.go
:
func (tg *testgoData) must(err error)
func (tg *testgoData) mustExist(path string)
func (tg *testgoData) mustNotExist(path string)
src/encoding/xml/xml.go
:
func (d *Decoder) mustgetc() (b byte, ok bool)
src/fmt/scan.go
:
func (s *ss) mustReadRune() (r rune)
src/reflect/value.go
:
func (f flag) mustBe(expected Kind)
func (f flag) mustBeExported()
func (f flag) mustBeAssignable()
src/syscall/dll_windows.go
:
func (d *LazyDLL) mustLoad()
func (p *LazyProc) mustFind()