Go常量的定义 发表于 2019-01-01 更新于 2026-04-17 分类于 Go 阅读次数: 常量的定义 使用 const,也可使用()一次定义多个常量。常量名称不必大写。go中不存在隐式类型转换,所以的类型转换都需要强制转换go 中枚举没有关键字,使用 const 定义即可,使用 iota 可达到自增的效果, iota 默认值为 0go 中没有char,只有 rune 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748package mainimport ( "fmt" "math")const filename string = "aaa.txt"func consts(){ const a, b = 3, 4 // Go 语言中类型转换是强制的,必须自己手动转 var c int // math.Sqrt 返回值为float,这里需手动转为 int c = int(math.Sqrt(a*a + b*b)) fmt.Println(filename, a, b, c)}const ( aa = 11 bb = 22)func enums(){ const ( cpp = 0 java = 1 python = 2 golang = 3 ) const ( javascript = iota _ php ) fmt.Println(cpp, java, python, golang) fmt.Println(javascript, php)}func main(){ consts() fmt.Println(aa, bb) enums()}