go 常量报错 const initializer is not a constant

Go的常量const是属于编译时期的常量,即在编译时期就可以完全确定取值的常量。只支持数字,字符串和布尔,及上述类型的表达式。而切片,数组,正则表达式等等需要在运行时分配空间和执行若干运算才能赋值的变量则不能用作常量。这一点和Java,Nodejs(javascript)不同。Java的final和Nodejs的const代表的是一次性赋值的变量,本质上还是变量,只是不允许后续再做修改,任意类型都可以,可以在运行时赋值。

可以这样类比:Go的常量对应于C#的const,而Java,Nodejs的常量对应于C#的readonly。


  1. package main
  2.  
  3. import(
  4. "regexp"
  5. )
  6.  
  7. //正确
  8. const R1 = 1
  9. const R2 = 1.02
  10. const R3 = 1 * 24 * 1.03
  11. const R4 = "hello" + " world"
  12. const R5 = true
  13.  
  14. //错误: const initializer ... is not a constant
  15. const R6 = [5]int{1,2,3,4,5}
  16. const R7 = make([]int,5)
  17. const R8 = regexp.MustCompile(`^[a-zA-Z0-9_]*$`)
  18.  
  19. func main(){
  20.  
  21. }
编译报错:
 
  1. ./checkconst.go:15:7: const initializer [5]int literal is not a constant
  2. ./checkconst.go:16:7: const initializer make([]int, 5) is not a constant
  3. ./checkconst.go:17:7: const initializer regexp.MustCompile("^[a-zA-Z0-9_]*$") is not a constant

转载:https://blog.csdn.net/pengpengzhou/article/details/105288976

原文地址:https://www.cnblogs.com/ithubb/p/14331131.html