Let’s GO! Part 4: Variables and Constants

Ali Azizjahan
4 min readDec 20, 2021

--

Variables are mutable but Constants are not.

OK let’s dive into the syntax!

Overview

Variables and Constants are the portions of the memory where you store your data in. These portions are named by you so you can access them later on.

How Do They Differ?

Well there are a few things that make these two different:

  • Variables are mutable but Constants are not. This means that you can change the value that a Variable holds at any time, as long as the the type matches. But you can’t do such thing with Constants. As soon as a Constant is defined and assigned a value, the value will remain the same until the end of the program and if you try to change it, you’ll get an error.
  • Variables are strictly typed but Constants can be remain untyped. This means once a Variable has gotten it’s type, you won’t be able to change it later on or assign a new value to it with another type. Letting go a Constant without a type will enable type conversion if needed and possible. For example if you assign an integer value to an untyped Constant, you can do math operators on it with another Variable or Constant that is of type float64 for example. But if you type it as int doing such operation would cause an error.
  • Constants can remain unused but Variables cannot. If you leave a Variable unused in your program, it won’t even compile! The GO compiler will tell you that you can’t leave Variables unused. But that is not the same for Constants. Define a Constant, assign it a value and if you never use it later on in your program, the compiler won’t even frown.
  • The Values of the Constants Should Be Known at the Build Time. Yes. That’s true. And that is exactly why you can’t use them to capture the return value(s) of the functions. Variables, on the other hand, can have values that are evaluated at run time and are technically unknown at build time.

Variables

There are two ways to define a Variable:

  • The shorthand way. You simply define a Variable and assign it a value at the same time without telling it what type it is. The type will be the type of the value that you assigned to it.
  • The long and verbose way. You first define a Variable and tell it what type it should be, then will assign a value to it later on.

Let’s see them both in action:

If you run it with go run main.go the output should be:

Hello, World!
Hi, Gopher!

Note: What’s happening on lines 14 and 20 is that we’re using placeholders in a string to format it. Those %v are the placeholders and you should pass the values to be replaced in the string in correct order as next arguments to the Printf function.

You can also define and mutate multiple variables all at once like this so it’s cleaner:

The output should be:

Hello, World!
Hi, Gopher!
12 - 13

Don’t worry about the types for now. We’ll cover them in detail on the future parts.

Constants

You can define one Constant or multiple ones at once just like Variables. But the syntax is somehow different. There are 3 possible syntaxes for Constants which you can see below:

The output should be:

Hello 12 Hi Gopher 32 23.54

Note: As already said before, the difference between typed and untyped Constants is that if you apply the mathematical operations on two Constants with different types when at least one of them is untyped, GO will try to convert the type of the untyped one or ones to the same type of the other one. If it’s not possible it’ll give errors. But if both of them are typed with different types it won’t be possible to do mathematical operations on them.

Where To Use Which?

Well, we mostly use Variables when writing programs in GO. Don’t think about Constants like you do in Javascript. In Javascript, as a convention for good practice, we use Constants to prevent ourselves from mutating them. Well that’s not the case in GO. Constants here are for defining values that are not going to change over the entire life cycle of our program. Take the Pi number (3.14159265359) as a good example for a Constant. It’s not going to change ever so we define it as a Constant. You won’t be able to assign anything that is meant to be a Variable to a Constant. For example you can’t assign the return values of a function to Constants since they’re meant to be Variables. This is due to the fact that we saw earlier that the values of the Constants should be known at build time. You’ll see this example in the next part.

Good job! You just learned about Variables and Constants.

Now you’re ready to move on to the next part which is Functions. Les goooo!

Next:

Previous:

List Index:

--

--

Ali Azizjahan

Software Engineer and Computers Researcher. Teaching you the things that I wish I knew myself sooner, or I could be taught way easier. https://linktr.ee/kyxey