A constant is an identifier of a simple value that will not be modified while the program is running.
Data types in constants can only be Boolean, numeric (integer, floating-point, and plural), and string.
The definition format of the constant:
const identifier [type] = value You can omit the type specifier [type] Because the compiler can infer the type of a variable based on its value
Explicit type definition:
const b string = "abc"Implicit type definition:
const b = "abc"
Multiple declarations of the same type can be abbreviated as:
const c_name1, c_name2 = value1, value2 The following example demonstrates the application of constants: The running result of the above instance is as follows: Constants can also be used as enumerations: The numbers 0, 1 and 2 represent unknown sex, female and male, respectively. Constants can be evaluated using the The running result of the above instance is as follows: A special The first The running result of the above instance is as follows: And look at an interesting The running result of the above instance is as follows: To put it simply: ITunes 1: move 0 bits to the left, and remain unchanged at 1. Jroom3: move 1 bit to the left to change to binary 110, or 6. Kroom3: move 2 bits to the left to change to binary 1100, or 12. Lumped 3: move 3 bits to the left to change to binary 11000, or 24. Note: 最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。 2.8.1. Example #
package main import "fmt" func main() { const LENGTH int = 10 const WIDTH int = 5 var area int const a, b, c = 1, false, "str" //Multiple assignments area = LENGTH * WIDTH fmt.Printf("The area is : %d", area) println() println(a, b, c) }
The area is : 50 1 false str
const ( Unknown = 0 Female = 1 Male = 2 )
len() , cap() , and unsafe.Sizeof() functions to evaluate the value of an expression. In a constant expression, the function must be a built-in function, otherwise it will not compile: 2.8.2. Example #
package main import "unsafe" const ( a = "abc" b = len(a) c = unsafe.Sizeof(a) ) func main(){ println(a, b, c) }
abc 3 16
Iota #
iota constant can be thought of as a constant that can be modified by the compiler. iota in const keyword will be reset to 0 ( const before the first line inside) const each new row of constant declaration in the will make iota count once ( iota can be understood as const index of rows in a statement block). iota can be used as an enumeration value:const ( a = iota b = iota c = iota )
iota is equal to 0 , and every time iota is used in a new line, its value will automatically increase by 1 ; so a=0 , b=1 , c=2 can be abbreviated as follows:const ( a = iota b c )
iota usage # 2.8.3. Example #
package main import "fmt" func main() { const ( a = iota //0 b //1 c //2 d = "ha" //Independent value,iota += 1 e //"ha" iota += 1 f = 100 //iota +=1 g //100 iota +=1 h = iota //7,Recovery Count i //8 ) fmt.Println(a,b,c,d,e,f,g,h,i) } 0 1 2 ha ha 100 100 7 8
iota example: 2.8.4. Example #
package main import "fmt" const ( i=1<<iota j=3<<iota k l ) func main() { fmt.Println("i=",i) fmt.Println("j=",j) fmt.Println("k=",k) fmt.Println("l=",l) }
i= 1 j= 6 k= 12 l= 24
iota indicates automatic addition of 1 from 0 , so i=1<<0 < span> , j=3<<1 < span> ( << means left shift) , i=1 , j=6 , which is not a problem. The key lies in k and l . From the output result to see k=3<<2 < span> , l=3<<3 < span> . <