GoLang: What Are Constants in Go and How Do You Use Them?
A constant is a constant and it’s constantly the same.
Get it?
Okay, that’s not nearly clear enough, especially when dealing with a programming language. Let me put it this way: once a constant is defined, its value cannot be changed.
But wait, don’t you define values like this:
|
1 |
var A int = 10 |
You 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:
|
1 2 3 4 5 6 7 8 9 10 |
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:
|
1 |
const A int = 1 |
Let’s plug that into our sample app like this:
|
1 2 3 4 5 6 7 8 9 10 11 |
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
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:
|
1 |
const A int = 1 vs. var A int = 1 |
The 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:
|
1 2 3 4 |
const ( FirstConstant string = "Hello," SecondConstant string = "New Stack!" ) |
That’s fairly straightforward. Let’s print the strings out in our main function like so:
|
1 2 3 |
func main() { fmt.Println(FirstConstant, SecondConstant) } |
Our entire program looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 |
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:
|
1 2 |
FirstConstant = "Yo" SecondConstant = "New Stack" |
Our entire app looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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)) } |
Here’s what we’ve done:
- 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
The output of the above code would be:
New Stack Math
2e+07
20000000
0.5623790762907029
That’s some serious New Stack math, my friends and, in each instance, none of our constants can be altered.
And that, my friends, is what Go constants are and how you use them. This feature will become your friend when you need to declare something that cannot be changed anywhere within your code.