Latest web development tutorials

Go language constant

Constant value is a simple identifier, the program is running, the amount will not be modified.

Constant data type can only be boolean, numeric (integer, floating point and complex numbers) and string.

Constants defined format:

const identifier [type] = value

You can omit the type specifier [type], because the compiler can infer the value of the variable to its type.

  • Explicit type definition: const b string = "abc"
  • Implicit type definition: const b = "abc"

A plurality of the same type of statement can be abbreviated as follows:

const c_name1, c_name2 = value1, value2

The following example demonstrates the application of the 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)   
}

Examples of the above operating results as follows:

面积为 : 50
1 false str

Constants can also be used to enumerate:

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

Numbers 0, 1 and 2, respectively, on behalf of unknown gender, women and men.

Constants can use len (), cap (), unsafe.Sizeof () to calculate the value of a constant expression. Constant expression, the function must be built-in functions, but otherwise the compiler:

package main

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

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

Examples of the above operating results as follows:

abc 3 16

iota

iota, special constant, can be considered a modification of the compiler can be constant.

When each of the const keyword appears, is reset to zero, and then the next before const appears once every iota, they represent numbers are automatically increased by one.

iota can be used as enumeration values:

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

The first iota equal to 0, whenever iota new line is used, it will automatically be added to the value of 1; therefore a = 0, b = 1, c = 2 can be abbreviated as follows:

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)
}

Examples of the above operating results as follows:

0 1 2 ha ha 100 100 7 8

Look iota of interesting examples:

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)
}

Examples of the above operating results as follows:

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

iota represents from 0 to automatically add 1, so i = 1 << 0, j = 3 << 1 (<< represents left shift means), namely: i = 1, j = 6, this is no problem, the key k and l, the output from the look, k = 3 << 2, l = 3 << 3.