.png?type=webp)
Constants in Go - Getting Started with Go
12 September, 2022
4
4
0
Contributors
In this article, we will briefly discuss constants in Go. You’ll read about typed and untyped constants in Go. In the last show, we discussed the basic data types. We have learned how variables are declared and the different data types like integers, floats, booleans, etc. If you are new to Go, I suggest you start your journey by starting this series. I am adding new articles every Monday to help you get started.
What will you learn in this Show?
🚧 Remember, this series is a quick start guide, and this will not cover the Go language completely. It’ll help you get to a certain point, but you need to do your research after it. I will add references to understand the language better at the end of the series.
You can expect to learn the following from this article,
- What are constant data types in Go
- How to declare constants in Go and why constants are important
- Typed and untyped constant in Go
Now that we have a basic outline of what to expect let’s dive into the article.
Constants in Go
Let’s first discuss constants in Go. If you come from JavaScript, C, or C++ background, I assume you already know what constants are. You can skip the first section if you want, but I would recommend reading this as well.
What is a Constant?
A constant is a way of defining an immutable value in Go. A constant in Go is declared by using the const
keyword. The official Go blog has a beautiful article about constants written by Rob Pike, one of the creators of Go. He defines the const
keyword in Go as the following:
In Go,
const
is a keyword introducing a name for a scalar value such as2
or3.14159
or"scrumptious"
. Such values, named or otherwise, are called constants in Go. Constants can also be created by expressions built from constants, such as2+3
or2+3i
ormath.Pi/2
or("go"+"pher")
. Source: Constants - The Go Blog
Let me break it down in easy words for you. To put it simply, a constant is an unchanging value. Once a value is declared using the const
keyword, you cannot change the value. But the constant in Go is more limited than in other languages. You can only declare a value in a constant that the compiler can check during the compile time. You cannot declare a value that can be calculated at runtime.
Declaring a Constant in Go
As discussed earlier, a constant in Go is declared using the const
keyword. You can declare it by following the structure const CONST_NAME = VALUE
. Let’s take a look at an example,
Output:
The above code snippet shows various possible ways of declaring a constant in Go. A constant can be declared in the package scope as well as in the function scope. We will discuss scopes in a later article. For now, just assume that a constant can be declared outside a function.
If you want to declare a constant along with its explicit type, you are welcome to do so by adding the type at the end. Line number 10 in the above code snippet is an example. And if you want to declare multiple constants at once, you can use a declaration list(shown in line 13).
But once you declare a constant, you cannot re-assign a value to it. Attempting to do so will return an error.
Typed and Untyped Constants
Constants can either be typed or untyped. We have already seen the examples of both.
Types of constants in Go
The concept of an untyped constant is the same as literal. An untyped constant doesn't have its type. Instead, the type is inferred from the value declared on the right-hand side.
The above declaration is an example of an untyped constant. An untyped constant must have a value. Otherwise, it’ll return an error saying missing constant value
(example).
Now that you know what an untyped constant is and how it is declared, you must’ve guessed what a typed constant is. A typed constant has its type defined. Here’s an example from the above code snippet:
If you try to define any other type of value to a typed constant, you’ll get a compile-time error.
Why Do We Need Constants?
There can be a few reasons we need constants in Go or any other programming language. Let’s discuss some reasons.
- Readability: Readability and correctness when running are the two most important features of a good code. The correctness can be checked automatically using unit tests or manually by checking the code by other developers. But bugs can keep hiding in the code if the code is not readable enough. Instead of using a repeated value at multiple positions in your codebase can make no sense to your fellow developer if he doesn’t know the context of the value. But if you use a constant and use the constant’s name at the required places, your code will become more readable.
- Maintainability: Writing maintainable code is also very crucial. If you are using only the constant name at different places, not entering a hard-coded value every time, changing the value at one place will change the value at all other places where the constant is used. It makes your code more maintainable.
- Code Optimization: Using constants also makes your code more optimized. For example, when the compiler knows that the value of a particular variable is not going to change, it can optimize the code. You can refer to this Stackoverflow answer to better understand this. Though the question refers to C and C++.
Naming Conventions in Go
👮♂️ This is a bonus section. You can skip it if you want.
Because we have already covered the ways of declaring variables and constants in Golang, let’s look at the naming conventions used in Go. The conventions are similar to other languages, but there are a few differences. Let’s discuss.
Like most programming languages, a variable or a constant in Go should start with a letter or an underscore. The name can contain underscores, numbers, or letters. But Go counts any Unicode character as a variable or constant name, considering Unicode counts the character as a letter or a digit.
The above code snippet has a variable that uses two Unicode characters. These are Bangla characters. But you can use any other character if it is a valid Unicode letter or a digit.
The above example shows you that you can use such characters, but using these variable names is not suggested. They are non-idiomatic and can cause problems for other developers.
You can also use underscores in a variable or constant name. But Go uses camel case(camelCase
) instead of snake case(snake_case
) for an identifier. So, following the camel case pattern when naming is a good practice.
The Go community also encourages using shorter variable names.
The smaller the variable scope, the shorter the name.
Even single-letter variable names are quite often used in Go.
It is also recommended to use short-hand names as long as they are understandable. For example, addr
is considered a good name.
🛑 Task: This code snippet has a few errors in it. Try to solve them, and comment your solutions here. This will test your knowledge from this and the last article.
Conclusion
The article's primary aim was to make you comfortable with constants in Golang. We have covered variables and constants till now. We have also discussed the popular naming conventions in this article. In the following article, we will dive deeper into the concepts of arrays, slices, maps, etc.
If you have any doubts or suggestions, please feel free to comment or DM me here or on Twitter.
golang
programming
go