Easy Tutorial
❮ Go Pointers Go Arrays ❯

Go Language Constants

A constant is an identifier for a simple value that does not change during program execution.

The data types of constants can only be boolean, numeric (integer, floating-point, and complex), and string.

The format for defining a constant is:

const identifier [type] = value

You can omit the type specifier [type] because the compiler can infer the type from the value of the variable.

Multiple declarations of the same type can be abbreviated as:

const c_name1, c_name2 = value1, value2

The following example demonstrates the use 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 assignment

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

The output of the above example is:

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 gender, female, and male, respectively.

Constants can be used in expressions calculated by functions like len(), cap(), and unsafe.Sizeof(). In constant expressions, the functions must be built-in; otherwise, the compilation will fail:

Example

package main

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

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

The output of the above example is:

abc 3 16

iota

iota is a special constant that can be considered as a constant that can be modified by the compiler.

When the const keyword appears, iota is reset to 0 (before the first line within the const block), and each new line in the const block increments iota by 1 (iota can be understood as the line index within the const block).

iota can be used as an enumeration value:

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

The first iota equals 0, and its value increments by 1 each time it is used on a new line; so a=0, b=1, c=2 can be abbreviated as:

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, resume counting
        i          // 8
    )
    fmt.Println(a, b, c, d, e, f, g, h, i)
}

The output of the above example is:

0 1 2 ha ha 100 100 7 8

Here's 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 output of the above example is:

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

iota starts from 0 and increments by 1, so i=1<&lt;0, j=3<&lt;1 (<< denotes left shift), which means i=1, j=6. The key is in k and l, where the output shows k=3<&lt;2, l=3<&lt;3.

Simple explanation:

Note: <&lt;n is equivalent to multiplying by 2^n.

❮ Go Pointers Go Arrays ❯