Typically a function prefixed with Must indicates that should a problem occur, the program will panic rather than return an error. Functions with the must prefix are usually used in one of the following situations:
use of constant-like behavior, but cannot be defined as a constant (such as regex parse). The pattern string may be constant, and a single test is enough to assert that no panic will ever occur.
unrecoverable operation, error handling would just add extra boilerplate, and likely not very useful. This is common during application startup.
MustCompile is like Compile but panics if the expression cannot be parsed. It simplifies safe initialization of global variables holding compiled regular expressions.
Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
This is similar in that, there's no "handling" of the condition. If the condition is not met, control is not returned to the caller. The test is just skipped.
I sprig I see:
regexFind panics if there is a problem and mustRegexFind returns an error to the template engine if there is a problem.
Typically a function prefixed with
Must
indicates that should a problem occur, the program will panic rather than return an error. Functions with themust
prefix are usually used in one of the following situations:use of constant-like behavior, but cannot be defined as a constant (such as regex parse). The pattern string may be constant, and a single test is enough to assert that no panic will ever occur.
unrecoverable operation, error handling would just add extra boilerplate, and likely not very useful. This is common during application startup.
Examples of use of
must
:https://pkg.go.dev/regexp#MustCompile
https://pkg.go.dev/text/template#Must
MustHaveGoBuild
https://go.dev/src/internal/testenv/testenv.goThis is similar in that, there's no "handling" of the condition. If the condition is not met, control is not returned to the caller. The test is just skipped.
I
sprig
I see: