1. 程式人生 > 其它 >Go-常量-const

Go-常量-const

常量:只能讀,不能修改,編譯前就是確定的值

關鍵字: const

常量相關型別:int8,16,32,64 float32,64 bool string可計算結果數學表示式

常量方法  iota

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 packagemain import"fmt" funcmain() { constname ="BeinMenChuiXue" fmt.Println(name) const( homeAddr =
"earth" nickName ="WeiLaiShiXuePai" ) fmt.Println(homeAddr, nickName) const( zero = iota one two = iota three ) fmt.Println(zero, one, two, three) const( first = iota second ) fmt.Println(first, second) const( student ="Golang" teacher parents ="S" children ) fmt.Println(student, teacher, parents, children)
}

  

觀察輸出結果,得出以下有關常量特性

  1.定義多個常量推薦使用括號 

  2. iota每往下一行自增1並賦值給這一行的常量,遇到下一個const重新從0開始

  3.常量未賦值,往上找最近有值的常量並賦值給自己

Go-常量-const