![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
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))
}