TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Go

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
Featued image for: GoLang: What Are Constants in Go and How Do You Use Them?
Feature image by Jas Min on Unsplash

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:


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:


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:


Let’s plug that into our sample app like this:


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:


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:


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:


That’s fairly straightforward. Let’s print the strings out in our main function like so:


Our entire program looks like this:


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:


Our entire app looks like this:


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:


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.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.