Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Constants

Go Language Constants

A constant is an identifier for a simple value, a quantity that is not modified during program execution. The data types of constants can only be Boolean, numeric (integer, floating-point, and complex), and string.

Word meaning: const Is constantAn abbreviation of (constant), representing an immutable value in a program.


Basic syntax and parameters

The format for defining a constant:



const identifier [type] = value

Syntax Description

Definition


Example

Example 1: Basic Constant Definition

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 assignment

   area

=

LENGTH

WIDTH

   fmt

.

Printf

(

"Area is : %d"

,

area

)

   

println

()

   

println

(

a

,

b

,

c

)

}

Expected output:



面积为 : 50

1 false str

Code analysis:

  1. const LENGTH int = 10 Defines an integer constant LENGTH with a value of 10.
  2. const WIDTH int = 5 Defines an integer constant WIDTH with a value of 5.
  3. const a, b, c = 1, false, "str" Uses multiple assignment to define multiple constants of different types at the same time.
  4. area = LENGTH * WIDTH Calculates the area and assigns it to the variable area.

Constant Groups and Enumeration

Go has no enum keyword; constant groups are usually used to achieve enumeration:

Example

package

main

import

"fmt"

// Define a gender enumeration

const

(

    Unknown

=

0

    Female  

=

1

    Male    

=

2

)

func

main

()

{

    fmt

.

Printf

(

"Unknown=%d, Female=%d, Male=%d\n"

,

Unknown

,

Female

,

Male

)

}

Expected output:



未知=0, 女性=1, 男性=2

Code analysis:


Application of Built-in Functions in Constants

Built-in functions such as len()、cap()、unsafe.Sizeof() can be used in constant expressions to compute values.

**Note:** Functions in constant expressions must be built-in functions.

Example

package

main

import

(

   

"fmt"

   

"unsafe"

)

const

(

    a

=

"abc"

    b

=

len

(

a

)

    c

=

unsafe

.

Sizeof

(

a

)

)

func

main

()

{

    fmt

.

Printf

(

"a=%s, b=%d, c=%d\n"

,

a

,

b

,

c

)

}

Expected output:



a=abc, b=3, c=16

Code analysis:


The iota Constant Counter

iota is a special constant in Go, also called the constant counter. It can automatically generate increasing sequence values in a const declaration block.

Key Features:

Example 1: Basic iota Usage

Example

package

main

import

"fmt"

// iota starts at 0 and increases by 1 each line

const

(

    a

=

iota  

// 0

    b          

// 1 (value omitted, using the previous line's iota by default)

    c          

// 2

)

func

main

()

{

    fmt

.

Printf

(

"a=%d, b=%d, c=%d\n"

,

a

,

b

,

c

)

}

Expected output:



a=0, b=1, c=2

Code analysis:

Example 2: Detailed 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

   

// Independent value, iota += 1

        g          

// 100,iota += 1

        h

=

iota  

// 7, resume using the iota value

        i          

// 8

   

)

    fmt

.

Printf

(

"a=%d, b=%d, c=%d, d=%s, e=%s, f=%d, g=%d, h=%d, i=%d\n"

,

a

,

b

,

c

,

d

,

e

,

f

,

g

,

h

,

i

)

}

Expected output:



a=0, b=1, c=2, d=ha, e=ha, f=100, g=100, h=7, i=8

Code analysis:

Example 3: iota and Bit Shift Operations

Example

package

main

import

"fmt"

const

(

   

i

=

1

<< iota  

// 1 << 0 = 1

    j

=

3

<< iota  

// 3 << 1 = 6

    k              

// 3 << 2 = 12

    l              

// 3 << 3 = 24

)

func

main

()

{

    fmt

.

Printf

(

"i=%d, j=%d, k=%d, l=%d\n"

,

i

,

j

,

k

,

l

)

}

Expected output:



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

Code analysis:

Additional Notes