Tutorial | Go Language | Constant In Golang 2019

Check This Previous blog post written by Sagar Jaybhay for better understanding.

Constant in Go language / Golang

In the below program we create the variable x and assign value 10 after this we again assign value 12 to the same variable x and we print this on the console which is working fine. By using this you will know we can change the value of variable anytime in our program.

package main

import (
    "fmt"

    "./utilitypackage"
)

func main() {

    //fmt.Println(utilitypackage.Reverse("sagar"))
    //utilitypackage.CreateDataTypes()

    var x = 10
    fmt.Println("val is x", x)

    x = 12
    fmt.Println("changed val is x", x)

    fmt.Println("\n called in main ", utilitypackage.Name)

}

General Variable In Golang
General Variable In Golang

Now we will create constant and check what will happen to see below image :

Constant In Golang
Constant In Golang

Below is the program for Constant

package main

import (
    "fmt"

    "./utilitypackage"
)

func main() {

    //fmt.Println(utilitypackage.Reverse("sagar"))
    //utilitypackage.CreateDataTypes()

    const x = 10
    fmt.Println("val is x", x)

    x = 12
    fmt.Println("changed val is x", x)

    fmt.Println("\n called in main ", utilitypackage.Name)

}

When you declared a constant in your program you can’t change the value of this variable again in your program. If you try to change the value of this it will throw an error.

Point to remember when you declared a constant you need to assign value to that variable at the time of declaration else it will throw an error.

See below image

Constant In golang reassign value error
Constant In golang reassign value error

Different way’s to declare constant variables in golang

package main

import "fmt"

const b string = "sagar jaybhay"
const a = 42

const (
    v = iota
    j = iota * 10
    k = iota * 10
)

func main() {

    const aa = "sagar"
    fmt.Println("hello brother")
    println(v)
    println(j)
    println(k)
}

Iota

In greek language, iota means very small value and in the above program when we declared v=iota it stands for v=0 and next j=iota*10 here iota becomes 1 so our j value 1*10=10 and so on.

Different Ways to declare Constant with iota in golang
Different Ways to declare Constant with iota in golang

Arithmetic Operation:

You will able to do all arithmetic operation on number type variables like addition, subtraction, division.

GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...