In the context of a Go declaration, assigning with a simple = creates a constant, not a variable. (Outside of the declaration, this is a variable assignment that must already exist.)
But constant initialization should only include constants - not calls like regexp.MustCompile() - so pattern cannot be a constant in this case, even if you do not plan to change its value later. (In fact, even if you somehow initialized it without invoking anything, Regexp cannot be a constant in Go, there can only be basic types.)
This means that you need to make it a variable, either by placing it in the var statement, or by declaring it inside the function with := instead of = :
var ( pattern = ... )
or
var pattern = ...
or
func something() { pattern := ... }
source share