Go language constants


A constant is an identifier of a simple value that will not be modified when the program is running.

The data types in constants can only be Boolean, numeric (integer, floating point and complex numbers) and string types.

Definition format of constants:

const identifier [type] = value

You can omit the type specifier [type] because the compiler can infer the type of the 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 examples demonstrate the application of constants:

package main

import "fmt"

func main() {
   const LENGTH int = 10
   const WIDTH int = 5   
   var area int
   const a, b, c = 1, false, "str" //多重赋值

   area = LENGTH * WIDTH
   fmt.Printf("面积为 : %d", area)
   println()
   println(a, b, c)   
}

The result of the above example is:

面积为 : 50
1 false str

Constant can also be used as an enumeration:

const (
    Unknown = 0
    Female = 1
    Male = 2
)

The numbers 0, 1 and 2 represent unknown gender, female and male respectively.

Constants can use len(), cap(), unsafe.Sizeof() constants to calculate the value of expressions. In constant expressions, the function must be a built-in function, otherwise it will not compile:

package main

import "unsafe"
const (
    a = "abc"
    b = len(a)
    c = unsafe.Sizeof(a)
)

func main(){
    println(a, b, c)
}

The running result of the above example is:

abc 3 16


iota

iota, special constant, Can be thought of as a constant that can be modified by the compiler.

When each const keyword appears, it is reset to 0, and then before the next const appears, every time iota appears, the number it represents will automatically increase by 1.


iota can be used as an enumeration value:

const (
    a = iota
    b = iota
    c = iota
)

The first iota is equal to 0. Whenever iota is used in a new row, its value will be Automatically add 1; so a=0, b=1, c=2 can be abbreviated to the following form:

const (
    a = iota
    b
    c
)

iota Usage

package main

import "fmt"

func main() {
    const (
            a = iota   //0
            b          //1
            c          //2
            d = "ha"   //独立值,iota += 1
            e          //"ha"   iota += 1
            f = 100    //iota +=1
            g          //100  iota +=1
            h = iota   //7,恢复计数
            i          //8
    )
    fmt.Println(a,b,c,d,e,f,g,h,i)
}

The result of the above example is:

0 1 2 ha ha 100 100 7 8

Look at another interesting iota 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)
}

The result of the above example is:

i= 1
j= 6
k= 12
l= 24

iota means that it automatically increases by 1 starting from 0, so i=1<<0,j= 3<<1 (<< means left shift), that is: i=1, j=6. This is no problem. The key lies in k and l. From the output result, k=3<<2, l=3<<3.