Idiomatic Go error-handling pattern: Must

UUID v7 Must

Recently, when using the official Go uuid library to generate UUID v7, I noticed a usage pattern: Must🔗. Its description is simple:

Must returns uuid if err is nil and panics otherwise.
func Must(uuid UUID, err error) UUID {
if err != nil {
panic(err)
}
return uuid
}

The use case is “when an error is not a recoverable option”: use the wrapped Must to panic on the error directly.

// Before
id, err := uuid.NewV7()
if err != nil {
log.Fatalf("failed to generate UUID v7: %v", err)
}
// After
mustId := uuid.Must(uuid.NewV7())

Summary

Must is an idiomatic convention in the Go ecosystem for cases where an immediate panic is acceptable
var (
validEmail = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)
)
func init() {
templates = template.Must(template.ParseGlob("templates/*.html"))
}
  • Suitable for: initialization phase (init(), at the start of main(), global variable initialization). In these cases, panic equates to startup failure, and terminating the program with Fatal is reasonable.
  • Not suitable for: inside HTTP handlers, goroutines, or user request handling paths. A panic there would crash the whole service or complicate recover logic.

Further reading