Golang tutorial - 2 Constant

nadirbasalamah - May 17 '20 - - Dev Community

Constant in Golang

In many programming languages, there is a special variable called a constant. A constant is a variable that the value cannot be changed. To declare a constant variable in Go, use the const keyword.
The declaration of a constant can be seen in this code below.

const pi = 3.14               //1. using const keyword followed by constant name
const myConst string = "test" //2. using const keyword with specified type
const (
    a = 12
    b = 23
    c = 34
) //3. declare multiple constants easily with const(..) just like declaring variables
Enter fullscreen mode Exit fullscreen mode

Here is the complete code example using const.

package main

import "fmt"

const pi = 3.14               
const myConst string = "test" 
const (
    a = 12
    b = 23
    c = 34
) 
func main() {
    area := pi * 7 * 7
    fmt.Println("Area of circle : ", area)
    fmt.Println("Other const", myConst, a, b, c)
}
Enter fullscreen mode Exit fullscreen mode

The output from that code is :

Area of circle :  153.86
Other const test 12 23 34
Enter fullscreen mode Exit fullscreen mode

If we try to change a value from a const, this code throws an error

package main

import "fmt"

func main() {
    const a = 12
    a = 45 //change a value from const
    fmt.Println("value of a", a)
}
Enter fullscreen mode Exit fullscreen mode

The error message from that code :

.\main.go:7:4: cannot assign to a
Enter fullscreen mode Exit fullscreen mode

iota

There is a special const in Go called iota. iota is a const that has a default value equal to 0 and will increase automatically by 1 if assigned to another constant.

Here is the code example using iota.

package main

import "fmt"

const (
    a = iota // iota = 0
    b = iota // iota = 1
    c = iota // iota = 2
)

const d = iota // iota value reset to 0 because assign into another variable with const keyword

func main() {
    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(c)
    fmt.Println(d)
}
Enter fullscreen mode Exit fullscreen mode

The output from that code :

0
1
2
0
Enter fullscreen mode Exit fullscreen mode

Another example of using iota can be seen in this code.

package main

import (
    "fmt"
)

const (
    _      = iota      //ignore the default value with _ notation
    bronze = 10 * iota //iota = 1
    silver = 10 * iota // iota = 2
    gold   = 10 * iota // iota = 3

)

func main() {
    score1 := 200 * bronze
    score2 := 200 * silver
    score3 := 200 * gold
    fmt.Println("The scores : ", score1, score2, score3)
}
Enter fullscreen mode Exit fullscreen mode

The output from that code :

The scores :  2000 4000 6000
Enter fullscreen mode Exit fullscreen mode

Sources

Read more about constants in Go here

I hope this article helps to learn the Go programming language. If you have any thoughts or feedback, you can write it in the discussion section below.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player