In this Golang tutorial, you will learn all about Go constants. Golang constants are similar to variables except for the condition that once a variable is declared as constant it is not possible to make changes to its value. Under this tutorial, you will dive through what are constants in Go, the different syntax used for constant variable declaration, naming conventions, types of constants, and their multiple declarations.
A Go programming language constant is a variable whose value remains unchanged. The assigned value remains fixed or unchanged for the constant variable until the successful execution of the Golang program or till the user makes changes to the assigned value of the variable. Go language is read-only and cannot be modified.
In the Go programming language declaration of constant variables is simple and easy like any other variable declaration as we discussed in earlier tutorials.
const
declares a variable in the Go programming language as a constant. const
is followed by a variable name and its type to identify the type of value stored by the constant variable.Syntax1
Const <variable_name> <type> = <value>
const PLACE string = "Europe"
const street_no int = 3456
The syntax declares a constant variable with variable names eg PLACE, street_no, and assigns values to it depending on the type of value the constant variable holds.
package main
import "fmt"
const PLACE string = "Europe"
const street_no int =3456
func main() {
fmt.Println(PLACE)
fmt.Println(street_no)
}
const
makes Europe of type string and the number 3456 of integer type remains unchangeable during the execution of the Go program as constant variables.Output:
Europe 3456
Syntax 2:
Const <variable_name> = <value>
In this syntax, the type of constant variable is inferred during program execution time with no need of predefining the type of variable value contained by the constant variable.Here const keyword declares the variable as constants.This syntax is an implicit type.
const PLACE1 = “Germany”
const street1 = 45
package main
import "fmt"
const PLACE1=”Germany” //Type inferred
const street1 = 45 //Type inferred
func main() {
fmt.Println( PLACE1)
fmt.Println(street1)
}
Output:
Germany 45
Two types of Golang Constants
Typed Constants are Constant variables that are declared with a specific type. Typed Constants is also known as explicit typing.
package main
import ("fmt")
/* const declared outside a function */
const name string = "Typed Constant"
func main() {
fmt.Println( name)
}
Output:
Typed Constant
Untyped Constants are Constant variables declared with unknown types. Untyped Constants is known as implicit typing.
package main
import ("fmt")
/* const declared outside a function */
const z = 100
func main() {
fmt.Println(z)
}
Output:
100
Note: Compiler decides the constant variable type in case of untyped constants.
package main
import ("fmt")
func main() {
/* const declared inside function */
const value =10
const z = 3.14
const u = "Hi!"
fmt.Println(value)
fmt.Println(z)
fmt.Println(u)
}
Multiple constants are grouped into a block. Multiple constant variables are defined using the following syntax
Syntax
Const (
A = “ one“
B = 90
C = 78.90
D = false
)
The const keyword is followed with parenthesis “()” and inside the parenthesis defines multiple variables that are assumed to be constants
const (
PRODUCT = "Dress "
QUANTITY = 2
PRICE = 1550.50
STOCK = true
)
package main
import "fmt"
const (
PRODUCT = "Dress "
QUANTITY = 2
PRICE = 1550.50
STOCK = true
)
func main() {
fmt.Println(QUANTITY)
fmt.Println(PRICE)
fmt.Println(PRODUCT)
fmt.Println(STOCK)
}
Output:
2 1550.5 Dress true
No, redeclaration of constant variables results in compilation error.
Let us understand with an example
package main
import ("fmt")
func main() {
const value =10
const z = 3.14 // z declared as constant
const u = "Hi!"
var z = 3 // z redeclared as variable
fmt.Println(value)
fmt.Println(z)
fmt.Println(u)
fmt.Println(z)
}
Output:
#_/home/y4zQxY ./prog.go:8:9 z reclared in this book previous declaration at ./prog.go:6:15
Note: Constant variables cannot be redeclared which results in a compilation error