VOOZH about

URL: https://thenewstack.io/golang-what-are-constants-in-go-and-how-do-you-use-them/

⇱ GoLang: What Are Constants in Go and How Do You Use Them? - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

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.

What’s next?

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.

PREV
1 of 2
NEXT
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%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-04-29 17:00:54
GoLang: What Are Constants in Go and How Do You Use Them?
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 Jack Wallen
👁 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:
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:
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 = 1
Let’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 = 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:
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))
}
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.
TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.