Golang Constants – In this blog, we will learn about constants in Golang or The Go Programming Language.
Let’s see what we are going to cover in this Golang tutorial blog.
- Naming Convention of the constants in Golang
- Typed and un-typed Constants in Golang
- Enumerated constants and Enumeration Expression
Golang Constants Naming
Go Constants are named the same as we name variables i.e PascalCase or camelCase.
The only thing to take care of is we use PascalCasing when we need to export the variable to other packages, if not then we use camelCasing in Golang.
const myConst int = 17
fmt.Printf("%v,%T\n",myConst,myConst)
Output:
17, int
This is an example of typed constant in Golang.
Let’s see how its different form normal variables in Golang.
package main
import (
"fmt"
)
func main() {
const myConst int = 17
myConst = 27
fmt.Printf("%v,%T\n", myConst, myConst)
}
Error:- cannot assign to myConst
We can re-initialize a new value in a variable but we can’t change the value of constant once initialized.
The Constants in Go language must be assignable at the compile time of the program, else it will throw an error.
Example:
package main
import (
"fmt"
"math"
)
func main() {
var myConst float64 = math.Sqrt(4)
fmt.Printf("%v,%T\n", myConst, myConst)
}
Output:
2,float64
When we use the normal variable we get math.Sqrt(4) as 2. But when this is done for constants in Go language, we get an error.
package main
import (
"fmt"
"math"
)
func main() {
const myConst float64 = math.Sqrt(4)
fmt.Printf("%v,%T\n", myConst, myConst)
}
Error:- const initializer math.Sqrt(4) is not a constant
Sqrt() is a function of math package in Golang and inorder to get the square root of a number we need to run that function.
But in constants we need to initialize value in compile time so it throws an error.
Golang Constants
const a int = 17
const b float64 = 12.45
const c bool = true
const s string = "Go Constants"
fmt.Printf("%v,%T\n", a, a)
fmt.Printf("%v,%T\n", b, b)
fmt.Printf("%v,%T\n", c, c)
fmt.Printf("%v,%T\n", s, s)
Output:-
17,int
12.45,float64
true,bool
Go Constants,string
Golang Constants Scope
Constants are immutable, but can be shadowed.
package main
import (
"fmt"
)
const a int16 = 17
func main() {
const a int = 100
fmt.Printf("%v,%T\n", a, a)
}
Output:
100,int
Golang Operation between constant and non-constant variables
package main
import (
"fmt"
)
func main() {
const a int = 100
var b int = 17
fmt.Printf("%v,%T\n", a+b, a+b)
}
Output:
117,int
const a int = 100
var b int16 = 17
fmt.Printf("%v,%T\n", a+b, a+b)
Error: – invalid operation: a + b (mismatched types int and int16)
Cannot add variables of two different sizes.
Golang Untyped Constants
const a = 100
fmt.Printf("%v,%T\n", a, a)
Output:
100,int
The compiler itself infers the type of the variable by looking at the value.
package main
import (
"fmt"
)
func main() {
const a = 100
var b int16 = 17
fmt.Printf("%v,%T\n", a+b, a+b)
}
Output:
117,int16
Do you remember this example from the Go typed constants, that used to throw an error?
while using Go untyped constants the compiler gives it type and size with which operation is being done.
The variable ‘a’ is replaced with its value and the operation is done on its value and thus gets converted to the type of variable ‘b’.
Example, if we convert the size of variable ‘b’, let’s see what we get as output.
const a = 100
var b int32 = 17
fmt.Printf("%v,%T\n", a+b, a+b)
Output:
117,int32
var b int32 = 17
fmt.Printf("%v,%T\n", 100+b, 100+b)
Output:
117,int32
Thus, we can say that the variable ‘a’ gets replaced by its value.
We can do these kinds of implicit conversions only in golang constants and not possible in golang variables, to achieve this in variables we will have to do an explicit conversion.
Golang Const iota
Go Enumerated constants
package main
import (
"fmt"
)
const a = iota // Enumerated Constant
func main() {
fmt.Printf("%v,%T\n", a, a)
}
Output:
0,int
Go Const iota is just like a counter. we can use this when we are using Golang enumerated constants.
Golang Multiple Constants
package main
import (
"fmt"
)
// Enumerated Constant
const (
a = iota
b = iota
c = iota
d = iota
e = iota
)
func main() {
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
}
Output:
0
1
2
3
4
we can also sort this.
//constant block
const (
a = iota
b
c
d
e
)
Output:
0
1
2
3
4
const (
a = iota
b
c
d
e
)
const (
m = iota
n
o
p
q
)
Output:
0123401234
Both the constant blocks are separate and thus do not interfere with each other. Iota is scoped to constant blocks.
We can make use of golang iota function to make programs that uses counter.
Program to display Alphabets using ASCII values.
package main
import (
"fmt"
)
// Enumerated Constant
const (
_ = iota + 96
a
b
c
d
e
)
func main() {
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
fmt.Println(string(d))
fmt.Println(string(e))
}
Output:
a
b
c
d
e
‘_’ is used as a write-only variable. It doesn’t take memory in our program and we can’t read as it’s not stored.
Know more about golang constants in the official documentation of golang.