34_Go基础_1_1 const常量
1 package main 2 3 import "fmt" 4 5 func main() { 6 /* 7 常量: 8 1.概念:同变量类似,程序执行过程中数值不能改变 9 2.语法: 10 显式类型定义 11 隐式类型定义 12 13 3.常数: 14 固定的数值:100,"abc" 15 */ 16 fmt.Println(100) 17 fmt.Println("hello") 18 19 //1.定义常量 20 const PATH string = "http:www.baidu.com" 21 const PI = 3.14 22 fmt.Println(PATH) 23 //fmt.Println(PI) 24 25 //2.尝试修改常量的数值 26 //PATH = "http://www.sina.com" //cannot assign to PATH 27 28 //3.定义一组常量 29 const C1, C2, C3 = 100, 3.14, "haha" 30 const ( 31 MALE = 0 32 FEMALE = 1 33 UNKNOW = 3 34 ) 35 //4.一组常量中,如果某个常量没有初始值,默认和上一行一致 36 const ( 37 a int = 100 38 b 39 c string = "ruby" 40 d 41 e 42 ) 43 fmt.Printf("%T,%d\n", a, a) // int 100 44 fmt.Printf("%T,%d\n", b, b) // int 100 45 fmt.Printf("%T,%s\n", c, c) // string ruby 46 fmt.Printf("%T,%s\n", d, d) // string ruby 47 fmt.Printf("%T,%s\n", e, e) // string ruby 48 49 //5. 枚举类型:使用常量组作为枚举类型。一组相关数值的数据 50 const ( 51 SPRING = 0 52 SUMMER = 1 53 AUTUMN = 2 54 WINTER = 3 55 ) 56 57 }