Tutorial | Go Language | Constant In Golang 2019
Check This Previous blog post written by Sagar Jaybhay for better understanding.
- Part 1: https://sagarjaybhay.net/google-go-tutorial-by-sagar-jaybhay-blog-1/
- Part 2: https://sagarjaybhay.net/packages-and-golang-command-part-2/
- Part 3: https://sagarjaybhay.net/detail-datatypes-in-golang-you-should-know-2019/
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) }

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

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

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.

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