Go language constant


Release date:2023-09-09 Update date:2023-10-13 Editor:admin View counts:344

Label:

Go language constant

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:

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 running result of the above instance is as follows:

The area is : 50
1 false str

Constants can also be used as enumerations:

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

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

Constants can be evaluated using the 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:

Example

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 instance is as follows:

abc 3 16

Iota

A special 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
)

The first 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

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

The running result of the above instance is as follows:

0 1 2 ha ha 100 100 7 8

And look at an interesting iota example:

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 running result of the above instance is as follows:

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

iota indicates automatic addition of 1 from 0 , so i=1<<0 , j=3<<1 ( << 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 , l=3<<3 .

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: <<n==*(2^n) .

Powered by TorCMS (https://github.com/bukun/TorCMS).