Variables in Go

May 7, 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.


Now, in this video, we are going to be covering the topic of variables within go. Now, variables are effectively in-memory storage locations for bits of information that could change throughout the course of our application’s execution.

Now, in go, there are a number of ways to assign or create new variables.

One of which is to just simply create the name of the variable and then use this := syntax here to then instantiate and assign the value of that variable. So in this case, we’ll call it Hello world and then if we wanted to use this variable, we could then just replace the hello world string here with the name of the variable that we want to print out.

Now, with these changes made, let’s try and run this locally. So go run main.go And as you can see, it’s been able to successfully print out the value of the variable welcome that we have just defined.

Now, in this case, we have allowed the go compiler to infer the type of the variable welcome. If we wanted to be more explicit, we could choose the var variable declaration, and then the name of the variable we want to define under the type of the variable.

So in this case, we wanted to define the welcome variable and it’s going to be a type string. If we wanted to, we could assign this to a value hello world like so, and then we can remove the original by commenting it out.

Now one noticeable difference here is that we are not using := and that’s because we’re not using the short variable declaration syntax here, we’ve used a more verbose variable declaration here, and we’ve assigned it to the value hello world.

Now let’s try it and execute this in the terminal. So go run main.go and as you can see, there has been no meaningful impact to the way that our application runs. The only difference is that we’ve been more verbose when it comes to declaring our variable and we’ve explicitly set the type to string.

Cool. So we’ve used two different ways of instantiating a variable here that have been of the type string. Let’s take a look at another example and try and create an integer object. So let’s say my age we’ll use the short variable, declaration, which uses the := syntax. And then we’re saying this to 27 and then we can then use the println built-in function to then print this out.

So myAge like, so let’s go into the terminal and run go run main.go and as you can see here it has successfully assigned the variable to value 27, and we’ve been able to print out that value below.

Now, if we wanted to do something like add to the myAge variable, we could do myAge++ and that will increment the value of 27 by one.

Let’s test that out. It’s now 28 or we could do += 10 if I was to grow 10 years older to 37. We can do -= as well, or -- to decrement the value by one or -=10.

Cool. Now, again, we could follow the same approach as we did with the welcome variable and we could be a little bit more verbose and how we are defining this variable. So I could do myAge and then int, we could assign this to the value 27. And that would be the same as using the short variable declaration we have down below.

As you can see no meaningful change in the execution of our application, it’s just that we’ve been a little bit more verbose in specifying the type for myAge.


package main

func main() {
	var welcome string = "hello world"
	// welcome := "hello world"

	var myAge int = 27
	// myAge := 27
	myAge -= 10



Running Our Code

$ go run main.go
hello world