GO First Steps

GO First Steps

First program

Β·

4 min read

I started to learn Go. I was looking to learn a new language, specially a language not related to Microsoft. Not that I have anything against them, but most of my career I've been working with programming languages and other technologies developed by Microsoft and wanted to try something different.

I was debating myself between Rust and Go. I wanted to learn a typed compiled multi use language that was fast but also modern. Rust is the trend now, but I've heard you have to relearn many things so that you can please the compiler (more specifically the borrow checker) so that your applications are safe. I actually think that's cool. On the other hand, Go is also memory safe, but in a different way: it's a garbage collector language. Which means that you don't care what happens to the memory you allocate for a specific function after you finished using it, because the runtime does it for you - with a performance cost, obviously.

Last week I was having a conversation with my boss at work and he mentioned that we were using Go for some projects, so I decided it'd have a look at it. I like it very much so far.

I will write some notes on my learning. But I won't be too academic. If you want to be able to get it up and running quickly, this is your place. If you want the history, why, and so on, look somewhere else.

Installation

First thing we need to do is to install the Go compiler and all the tools associated. Thankfully, it's just one installer that we have to run. In my case, I'm running the old and good Linux Mint (it never fails so I love it).

We need to go to https://go.dev, where we can download a compressed file. For the steps on how to install it, go to https://go.dev/doc/install, and select your operating system. I followed the steps for Linux, but with some modifications:

In the first step I changed this:

 rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

to this:

sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

The second step tells you to execute this command:

export PATH=$PATH:/usr/local/go/bin

That's fine, but not enough. If you just run that, you'd be adding the path of Go's installation to the PATH variable, which will let you run the command go anywhere and make it work. However, that will only work in your current session. As soon as you close your terminal and re-open it, it won't work anymore and you'll have to run that command again.

If you want to avoid that, in the terminal go to your home folder, and edit the file called .bashrc with this command:

nano .bashrc

Go to the end of the file and append this:

export PATH=$PATH:/usr/local/go/bin

Save the file and you'll be good to go.

You can now restart your terminal and try this command to make sure that go is properly installed and configured.

go version

It will show you something like this:

go version go1.21.5

First program

To create a program, we need to run this command:

go mod init myprogram

With this we're calling Go and telling it to create a new module called "myprogram". Make sure to run this command inside the directory where you want your program to be created.

We will open that folder in a code editor. You'll see that there's a file called go.mod with something like this inside:

module myprogram

go 1.21.5

It seems to have some information about our project.

We will add a new file called main.go and we'll add some code there.

Code

package main

func main() {
    println("Free Palestine πŸ‰!")
}

This code is pretty easy to understand, but I'll try to explain it anyways.

The first line defines the name for the package we are creating. A package is like something that groups files and other packages. It can be thought as a directory in an operating system, or a namespace in C#. Go programs always start executing function main in package main.

The second line is the declaration of the main function which, as I just said, gets executed first.

Then we print in the terminal a very beautiful message, by using the println function that comes out of the box in Go.

Another version

package main

import "fmt"

func main() {
    fmt.Println("Free Palestine πŸ‰!")
}

This version does exactly the same than the previous one. The only difference is that we're using the function Println instead of println (note the capitalization). At this moment I don't know the difference between them, but I've heard that this new version is more versatile so it's the most used one. The Println function is part of another package called frm, which also comes out of the box. But, as it's another package, we need to import it. That's what we do in the third line.

Last words

Well, I guess that's it for now. I you made it to the end of this article, thank you. I will write another post soon writing about variables, constants and data types. Stay tuned.

Β