Basics of Go

In this post we are going to cover creating and using variables in Go. The concept is something that you should be used to from other programming languages but the way Go handles the creation of variables can be a bit confusing to some. I want this post to hopefully clear up any of the confusion and help you get up and running with the language quickly. Let’s get started.

Variable declaration

In this section we will create some variables. In Go there are two ways to create variables. Long declaration which will be more like what you are used to coming from languages like Java or C#. Then there is the short declaration which, during your time with Go, will be what you see a bit more. So follow along with me and let’s create our project.

$ mkdir go-basics
$ cd go-basics
$ go mod init github.com/yournamehere/go-basics
$ code .

Now we should have our project set up and we can start creating our main.go file.

package main

import "fmt"

func main() {

}

This is our template for our main.go file. This is the basic strucutre of any Go file. Let’s break this down real quick. We have on line 1 our package name. In our case this file belongs to the main package. If we were to create a directory called text and then create a main.go file in there we would want to use package text as the package name instead of main, even though we have a main.go file we don’t want it get confused with our main package. I know it can be confusing but the more you do it and use these concepts the easier it gets. We will continue on and on line 3 we have our import statement. Now this might look a bit different depending on the language you come from. That’s ok. Just know that when you want to import a package you need to put it here. In order to have multiple imports you will need to do this:

import (
    "fmt"
    "os"
)

If you use something like VS Code or Goland you will get automatic imports when you go to use any package in the standard library. Since most of these tutorials I will be writing will focus on the standard library you shouldn’t have to worry too much about adding the import yourself. However if you run into errors when trying to run your program then you will need to make sure you added the correct imports.

Next up is our main function. This is the entry point to our application. Just like in C# or Java we have a main method that executes our program we have a similar concept here in Go which is the main function. Now we can start adding some variables. Let’s see how to use the long declaration.

... // everything above or below this is unchanged

var gv string // creates a globally scoped variable `gv` of type string
g = "I am global"
func main() {
    fmt.Println(g)
}

This will create our first global variable. In Go the shortest named variable is the best. I could have named this globalVar but due to Go convention, I named it gv instead. This is something a lot of programmers have to get used to when switching to the language. Most of what you are taught when you are first learning how to program is that long variable names are great if it helps to explain what the variable is holding. Like playerHealth is better than p or just health. Go takes another approach and would use ph for playerHealth instead. Now we can run our program.

$ go run main.go

We should now see I am global printed to the terminal. Awesome. Let’s do something similar but this time with short declaration syntax.

...
var gv string
gv = "I am a global variable"

sv := "Second variable"

func main() {
    fmt.Println(g)
    fmt.Println(sv)
}

This is the short declaration. We use the := opeartor to assign both the value and the type. Before, with the long declaration style, we had to explicitly tell Go this variable will be of type string. With the short declaration style the Go compiler will infer the data type. If you come from TypeScript you will be familiar with this concept of the compiler inferring your data type for you. We set a string of Second Variable to sv which means that the data type of the sv variable is string. Let’s run our program now.

$ go run main.go

Oh no. We get an error! Why? Well the anwer is quite simple. The short declaration style can only be used inside block scope. Which means we have to declare that variable inside the main function. Or inside the function that we want to use it in. Let’s modify the code to fix this.

var gv string
gv = "I am a global variable"

func main() {
    sv := "Second variable"
    fmt.Println(g)
    fmt.Println(sv)
}

Now let’s run our code.

$ go run main.go

Now when we run the code both strings should be printed to the terminal. How awesome. When I first started learning Go I had a bit of a hard time wrapping my head around short declaration syntax. I wasn’t sure when to use it or when not use it. Now I pretty much only use it unless I need to create a global variable. Most of the time you will find short delcaration as the preferred way of handling variable declaration in Go but that isn’t always the case of course.

Conclusion

I hope that this post was able to shed some light on Go’s use of variables and how to declare them. If you have ever looked at Go and wondered why there is a := between what looks like a variable name and the value held within you now know. The more and more I use Go the more I love how it handles things. I do believe that Go is a great language and can be used for so many things. This was only one post of many on the journey to learning Go. I hope you stick around. I will be creating a YouTube channel so if you learn better with video instruction then just know that it is coming. I want to get the posts out ASAP but we will cover the same content. Stay tuned.