I'm trying to write a regex in Golang to verify that a string only has alphanumerics, periods, and underscores. However, I'm running into an error that I haven't seen before and have been unsuccessful at Googling.
我正在尝试在Golang中编写一个正则表达式来验证字符串是否只有字母数字,句点和下划线。但是,我遇到了一个我以前没有见过的错误,并且在Googling上没有成功。
Here's the regex:
这是正则表达式:
pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
Here is the error:
这是错误:
const initializer regexp.MustCompile("^[A-Za-z0-9_\\.]+") is not a constant
What does "not a constant" mean and how do I fix this?
Thanks.
什么“不是常数”是什么意思,我该如何解决这个问题?谢谢。
3 个解决方案
#1
11
This typically happens when you're trying to assign to a package-level variable that has a type that can't be constant (like for example, Regexp
). Only basic types likes int
, string
, etc. can be constant. See here for more details.
当您尝试分配具有不能为常量的类型的包级变量时(例如,Regexp),通常会发生这种情况。只有基本类型如int,string等可以是常量。有关详细信息,请参见此处
Example:
pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
// which translates to:
const pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
You have to declare it as a var
for it to work:
你必须将它声明为var才能工作:
var pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
In addition, I usually put a note to say that the variable is treated as a constant:
另外,我通常会说明变量被视为常量:
var /* const */ pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
#2
5
The error is pretty clear. If you are trying to do this globally...
错误非常清楚。如果你想在全球范围内这样做......
Don't do:
const pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
Instead do:
var pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
Or if you really want the pattern in a constant:
或者如果你真的希望模式保持不变:
const pattern = `^[A-Za-z0-9_\.]+`
var alphaNum = regexp.MustCompile(pattern)
#3
4
In Go declaration context, assignment with a simple =
creates a constant, not a variable. (Outside of a declaration, it's an assignment to a variable that must already exist.)
在Go声明上下文中,使用simple =赋值会创建常量,而不是变量。 (在声明之外,它是对必须已存在的变量的赋值。)
But a constant initialization has to include only constants - not calls like regexp.MustCompile()
- so pattern
can't be a constant in this case, even if you don't plan on changing its value later. (In fact, even if you could somehow initialize it without calling anything, a Regexp
can't be a constant in Go; only basic types can be.)
但是,常量初始化必须只包含常量 - 而不是像regexp.MustCompile()这样的调用 - 因此,在这种情况下,模式不能是常量,即使您不打算稍后更改其值。 (事实上,即使你能以某种方式初始化它而不调用任何东西,Regexp也不能成为Go中的常量;只有基本类型才可以。)
That means you need to make it a variable by either putting it inside a var
statement or declaring it inside a function with :=
instead of =
:
这意味着您需要通过将其置于var语句中或在函数内使用:=而不是=声明它来使变量成为变量:
var (
pattern = ...
)
or
var pattern = ...
or
func something() {
pattern := ...
}
#1
11
This typically happens when you're trying to assign to a package-level variable that has a type that can't be constant (like for example, Regexp
). Only basic types likes int
, string
, etc. can be constant. See here for more details.
当您尝试分配具有不能为常量的类型的包级变量时(例如,Regexp),通常会发生这种情况。只有基本类型如int,string等可以是常量。有关详细信息,请参见此处
Example:
pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
// which translates to:
const pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
You have to declare it as a var
for it to work:
你必须将它声明为var才能工作:
var pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
In addition, I usually put a note to say that the variable is treated as a constant:
另外,我通常会说明变量被视为常量:
var /* const */ pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
#2
5
The error is pretty clear. If you are trying to do this globally...
错误非常清楚。如果你想在全球范围内这样做......
Don't do:
const pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
Instead do:
var pattern = regexp.MustCompile(`^[A-Za-z0-9_\.]+`)
Or if you really want the pattern in a constant:
或者如果你真的希望模式保持不变:
const pattern = `^[A-Za-z0-9_\.]+`
var alphaNum = regexp.MustCompile(pattern)
#3
4
In Go declaration context, assignment with a simple =
creates a constant, not a variable. (Outside of a declaration, it's an assignment to a variable that must already exist.)
在Go声明上下文中,使用simple =赋值会创建常量,而不是变量。 (在声明之外,它是对必须已存在的变量的赋值。)
But a constant initialization has to include only constants - not calls like regexp.MustCompile()
- so pattern
can't be a constant in this case, even if you don't plan on changing its value later. (In fact, even if you could somehow initialize it without calling anything, a Regexp
can't be a constant in Go; only basic types can be.)
但是,常量初始化必须只包含常量 - 而不是像regexp.MustCompile()这样的调用 - 因此,在这种情况下,模式不能是常量,即使您不打算稍后更改其值。 (事实上,即使你能以某种方式初始化它而不调用任何东西,Regexp也不能成为Go中的常量;只有基本类型才可以。)
That means you need to make it a variable by either putting it inside a var
statement or declaring it inside a function with :=
instead of =
:
这意味着您需要通过将其置于var语句中或在函数内使用:=而不是=声明它来使变量成为变量:
var (
pattern = ...
)
or
var pattern = ...
or
func something() {
pattern := ...
}