Learnmonkey Learnmonkey Logo

Go Constants

What is a Constant?

In Go, constants are variables, but you cannot change them.

Why use Constants?

You might think that constants are incredibly useless and no one should use them, but they are actually incredibly useful. Imagine that you were programming something that would calculate the circumference of a circle and you made a pi variable. If you accidentally change the value of pi, your calculations would be wrong. Instead, you could've made pi into a constant, so when you try to change the value of pi, you get an error.

Making a Constant in Go

Instead of typing something like var n int = 3 you type const n int = 3. Now, whenever you try and set the value of n, like n = 4, you get an error.