loading...

Golang tutorial - 2 Constant

nadirbasalamah profile image nadirbasalamah ・2 min read

Golang Tutorial (9 Part Series)

1) Golang tutorial - 1 Introduction 2) Golang tutorial - 2 Constant 3 ... 7 3) Golang tutorial - 3 Control Flow (Selection) 4) Golang tutorial - 4 Control Flow (Iteration) 5) Golang Tutorial - 5 Array, Slice and Map 6) Golang tutorial - 6 Function 7) Golang tutorial - 7 struct and interface 8) Golang tutorial - 8 Error Handling 9) Golang tutorial - 9 Concurrency using goroutine

Constant in Golang

In many programming languages, there is a special variable called constant. Constant is a variable that the value cannot be changed. To declare a constant variable in Go, use const keyword.
The declaration of 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

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

The output from that code is :

Area of circle :  153.86
Other const test 12 23 34

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

package main

import "fmt"

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

The error message from that code :

.\main.go:7:4: cannot assign to a

iota

There is a special const in Go called iota. basically, iota is a const that has a default value equals 0 and will increased automatically by 1 if assigned into another constant.

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

The output from that code :

0
1
2
0

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

The output from that code :

The scores :  2000 4000 6000

Sources

Read more about constant in Go here

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

Golang Tutorial (9 Part Series)

1) Golang tutorial - 1 Introduction 2) Golang tutorial - 2 Constant 3 ... 7 3) Golang tutorial - 3 Control Flow (Selection) 4) Golang tutorial - 4 Control Flow (Iteration) 5) Golang Tutorial - 5 Array, Slice and Map 6) Golang tutorial - 6 Function 7) Golang tutorial - 7 struct and interface 8) Golang tutorial - 8 Error Handling 9) Golang tutorial - 9 Concurrency using goroutine

Posted on May 17 by:

nadirbasalamah profile

nadirbasalamah

@nadirbasalamah

A guy who interested in web development and software engineering

Discussion

markdown guide