GoLang: What Are Constants in Go and How Do You Use Them?
The only constants in life are death and taxes, the saying goes. But in GoLang, any constant can be defined and impossible to change. Here's how.
Apr 29th, 2024 5:00pm by
Feature image by Jas Min on Unsplash
var A int = 10You certainly do. But if you were to define A in such a way, you could also change the value of A. Here’s an example:
package main
import ("fmt")
func main() {
var A int = 1
fmt.Println(A)
A = 2
fmt.Println(A)
}
What we’ve done above is define the variable A as an integer and set it to 1. Print A and you’ll get 1 in the output. After that, we redefine A as 2, and, guess what… 2 is the output.
We’ve changed the value of a variable.
But what if you wanted to make sure that couldn’t happen?
I know what you’re thinking. Just make sure you never change A in your code. But what happens when you’re code gets very large or maybe you’re collaborating on the code and you want to ensure A is never anything other than 1? In other words, you want that variable to be read-only.
For that, you need a constant.
In the Go programing language, constants are declared using the const keyword.
Constants can have character, string, Boolean, and numeric values. You can define a single constant or define them in a block of brackets.
First, let me show you how to define an integer constant and what happens if you attempt to change it. Remember our sample app above? I’m going to use that but I will define A as a constant with the statement:
const A int = 1Let’s plug that into our sample app like this:
package main
import ("fmt")
const A int = 1
func main() {
fmt.Println(A)
A = 2
fmt.Println(A)
}
If you run the above code, you’ll see the following error:
./constant.go:9:3: cannot assign to A (neither addressable nor a map index expression)
The error clearly indicates A is not addressable (read-only).
Huzzah, constants.
That doesn’t mean you can’t use the constant. For example:
package main
import ("fmt")
const A int = 1
func main() {
fmt.Println(A)
var B int = (A + 1)
fmt.Println(B)
}
The output of the above would be:
1
2
How did that happen? We didn’t change A, we just added to it, so A remained read-only and we defined a new variable with B.
So, the difference is:
const A int = 1 vs. var A int = 1The former is a constant, whereas the latter is a regular variable definition. But what about strings? You can also define a string with const. This time we’re going to define our constants in a block (as opposed to one at a time). Our constants are defined like so:
const ( FirstConstant string = "Hello," SecondConstant string = "New Stack!" )That’s fairly straightforward. Let’s print the strings out in our main function like so:
func main() {
fmt.Println(FirstConstant, SecondConstant)
}
Our entire program looks like this:
package main
import "fmt"
const (
FirstConstant string = "Hello,"
SecondConstant string = "New Stack!"
)
func main() {
fmt.Println(FirstConstant, SecondConstant)
}
If we run that app, the output would be:
Hello New Stack
Now, let’s try to alter one of those constants. We do that within our main() like this:
FirstConstant = "Yo" SecondConstant = "New Stack"Our entire app looks like this:
package main
import "fmt"
const (
FirstConstant string = "Hello"
SecondConstant string = "New Stack"
)
func main() {
fmt.Println(FirstConstant, SecondConstant)
FirstConstant = "Yo"
SecondConstant = "New Stack"
}
If we run the above, we’ll get the following errors:
./replace.go:14:6: cannot assign to FirstConstant (neither addressable nor a map index expression)
./replace.go:15:6: cannot assign to SecondConstant (neither addressable nor a map index expression)
Go caught the constants and knew they could not be altered. Even better, it doesn’t matter where you use the constant in your code, once it is defined, it cannot be changed.
You can also declare constants within func main(). In fact, you can declare them before and inside it. Here’s an example:
package main
import (
"fmt"
"math"
)
const A string = "New Stack Math"
func main() {
fmt.Println(A)
const B = 1000
const C = 2e10 / B
fmt.Println(C)
fmt.Println(int64(C))
fmt.Println(math.Cos(B))
}
- Defined a string for A that is “New Stack Math”
- Defined a constant for B that is 1000
- Defined a constant for C that is a very large number divided by B
- Printed C
- Printed C as a 64-bit integer
- Printed the Cosine of B
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.