💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
### 声明常量 #### 第一种 ~~~ const NAME string = "Jack ~~~ #### 第二种 ~~~ const name = "Jack" //自动推导类型 ~~~ #### 第三种 ~~~ const ( NAME string = "Jack" AGE = 20 //也可以自动推导类型 ) ~~~ ### iota(枚举) 常量计数器,iota遇到const就会重置为0 ~~~ const ( NUM1 = iota NUM2 //可以不写=iota NUM3 ) fmt.Println(NUM3) const NUM4 = iota fmt.Println(NUM4) const ( I = iota J, K, M = iota, iota, iota //可以卸载一行,值都一样 N = iota ) fmt.Println(I, J, K, M, N) ~~~ ~~~ 2 0 0 1 1 1 2 ~~~ ## 无类型常量 虽然一个常量可以有任意有一个确定的基础类型,例如int或float64,或者是类似time.Duration这样命名的基础类型,但是许多常量并没有一个明确的基础类型。编译器为这些没有明确的基础类型的数字常量提供比基础类型更高精度的算术运算;你可以认为至少有256bit的运算精度。这里有六种未明确类型的常量类型,分别是无类型的布尔型、无类型的整数、无类型的字符、无类型的浮点数、无类型的复数、无类型的字符串。 ~~~ const width = 3 const height = 4 fmt.Printf("%T\n", width) fmt.Printf("%T\n", height) sqrt := math.Sqrt(width*width + height*height) fmt.Printf("%T", sqrt) ~~~ 结果: ``` int int float64 ```