My new course Building a Production-Ready Service in Go is almost complete - try it out and let me know what you think!

Video:

Constants in Go

December 28, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Transcription

In the last video we looked at how we could define some different types of variables. So we looked at, we could define that the string type variable and the integer type variable. In this video, we’re going to be looking at a concept known as constant.

So constants are a little different from variables. They are effectively in-memory locations that cannot be changed throughout the course of our applications execution. This is really useful to us as programmers. Say, for example, you were doing a mathematical application, and you wanted to store the value of pi, for example, then using a constant here makes sense as the value of pi is never going to change no matter what happens within your application.

Now, when it comes to defining constants, we cannot use the shorthand variable declaration, syntax that we covered in the last video. Instead we have to use the more verbose option like this.

And the quick way to convert welcome to a constant is to change a var to const like, so. Now, from a practical standpoint, this does not change the execution of our application. It will still print out “hello world”.

However we now have that guarantee that the welcome string will never be changed throughout the course of this application’s execution.

Now, so far, we’ve only covered two distinct types. We’ve looked at the integer and string. However, if we were to store something like the value of PI. So let’s update this constant to pi

We could change the type of this to float32 and then use 3.14, which is the value of PI.

If we were to run this, go run main.go, you’ll see that this has been stored as a float 32 variable, and it has printed out this value here.

Now, one other cool thing you should know about constants before you go, is that we can leave them as untyped. So I can remove this type definition from the instantiation of our constant here. And we would still be able to run our application. However, the compiler would infer the type from this value.

Now, leaving this untyped is beneficial if we have applications that use different sizes of floats. So for example, if I had a size variable that was using the float 64 type then under the covers, the go compiler will automatically make the conversion from this untyped constant to type float64, and we’re not going to see any compiler mismatch errors that we would see otherwise.

So pi times size, Let’s go and try and run this so go run main.go as you can see everything’s good. However, if we were to leave this as typed, you should see that yup, mismatch types float 64 and float 32. So leaving this untyped in this example is going to be beneficial for us.

Code

package main

func main() {
	const pi = 3.14
	println(pi)

	var size float64 = 1.0
	println(size * pi)
}

Output

$ go run main.go
+3.140000e+000
+3.140000e+000