package main
import "fmt"
// const 常量: 不允许修改值
func main() {
const name string = "China"
fmt.Println("name is ", name)
const (
//BEIJING = iota //iota = 0 ,iota只能配合const()一起使用,变量无法使用;
BEIJING = 10 * iota //iota第一个 是0
SHANGHAI // iota = 1 第二个是1
SHENZHEN // iota = 2
)
fmt.Println("BEIJING = ", BEIJING)
fmt.Println("SHANGHAI = ", SHANGHAI)
fmt.Println("SHENZHEN = ", SHENZHEN)
const (
a, b = iota + 1, iota + 2 //iota = 0, a = 1, b = 2
c, d // iota = 1, c = iota + 1 = 2, d = iota + 2 = 3
e, f // iota = 2, e = 3, f = 4
g, h = iota * 2, iota * 3 // iota =3 , g = 6, h = 9
i, j // iota = 4, i = 8, j = 12
)
fmt.Println("a = ", a, "b = ", b)
fmt.Println("c = ", c, "d = ", e)
fmt.Println("e = ", e, "f = ", f)
fmt.Println("g = ", g, "h = ", h)
fmt.Println("i = ", i, "j = ", j)
}
Golang 常量
于 2023-04-03 10:43:03 首次发布