Learning GO (Golang): First steps and the mighty "Hello world"
Tags: googlecloud, beginners, programming, go
Published: 2022-02-01 21:26:28

These blog post are going to be me showing how i am learning **Go**, you are going to be part of my travel and i will take advantage of all of you by making sure i am learning the concepts. SO let's start this Journey.

***********************************************************
## A little background
I **always** like to start by understanding why something is used for, what are the possibilities with certain technologies or languages and who is using it.

###Let's get started by writing some of the main characteristics of the Go language:
* **Open Source** programming language.
* It was created at **Google** in 2007, anounced in 2009 and was released publicly in 2012 (So it has been out there for a while). And was designed thinking in merge the strenghts of **C** language and the easy syntax of languages like python.
* It is a compiled, statically typed language. These two aspects represents a really good learning opportunity as i come from python.
* Rich [package library](https://golang.org/pkg/). Well, it is less rich than python's library but it is growing.
* It was designed thinking on performance, it will use your resources as efficiently as possible, specially **CPU**.
* Provides Goroutines and channels to deal with concurrency.
* **Kubernetes** is written in GO. As a DevOps engineer this is really interesting. 
* Go generates binaries for your applications with all the dependencies built in, avoiding the need to install runtimes, and also this make GO multiplatform, so code developed on linux can run on MAC or WIndows only by having GO installed on those systems.
* It is in the **TOP 10** of the most loved languages in [stackoverflow](https://insights.stackoverflow.com/survey/2021)

**********************************************************
##All these features look nice, but then i moved to another question, what is GO used for??

###According to [GO official website](https://go.dev/solutions/#use-cases) you can use Go for:

![Use-cases-for-go](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2finqk8k3w2rqjafivy.png)
*Source*:[GO Use cases](https://go.dev/solutions/#use-cases)

 What jumps to my attention was the cloud aspect, so digging a little further i have found reference about using **GO** for:
* Networking automation
* CLoud scripts
* MAchine learning, and several engineer has been using GO for Data Science.

But related to this aspect, the most interesting part is that **GO** really excels on infrastructure stuff, this is why [Docker](https://www.docker.com/), [Kubernetes](https://kubernetes.io/) and [Prometheus](https://prometheus.io/) are build on GO.

***********************************************************
##Really cool features and use cases, but who is using it?
This was my third question, i wanted to know if learning Go will help me on my current job or if it will helpful in the future.

So let's travel again to the oficial GO website and look who is using the language.

![CLients-go](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9s9imnrc9c9ppbzg49z.png)
*Source*:[Who is using GO](https://go.dev/)

Very interesting, Google was the creator so it should be using it, and the rest of companies using it are important ones. These companies using this language let us know how important it is, and taking a look in pages like LinkedIn you can also notice an increment on GO developers requirements.

Now, i was convinced, this was a good technology to learn, so i started this journey.

***********************************************************
## First of all, install Go

I have always think that infrastructure and architectura subjects can be learn by studying, but coding requires practicing. THe first step is to install GO, get in touch with the process and lear where are the bins stored and how the language works.

As i saw in several sites, it is recommended to use **GO on Linux**, it is not mandatory just a recommendation. I will be installing GO 1.17 (*At the date of this post, version 1.18 i think was already release, but this 1.17 is stable).

For the OS, i am using Using **Ubuntu 20.04** on a laptop and for coding i am using **Visual Studio Code**, i have more experience using this software and the GO plugin is really good.

Let's get started, i follow the steps on the [official documentation for Linux installation](https://go.dev/dl/)

After the software was downloaded i follow this steps:
[GO Installation guide](https://go.dev/doc/install)

**Note** you can download your installer anywhere you want, but you should decompress it on the route `/usr/local/bin`

** Note ** using this steps yo should log out and them login back so the `.profile` get the changes on the path.

You can test the installation was correct by running:
```
(base) ernestol@ubuntu:~/go$ go version
go version go1.17.6 linux/amd64
```

### Now you need to configure your environment for developement

After installing go i created 3 directories under `home/go` (*you should also create the go folder*)

```bash
(base) ernestol@ubuntu:~$ mkdir go
(base) ernestol@ubuntu:~$ cd go/
(base) ernestol@ubuntu:~/go$ mkdir bin pkg src
(base) ernestol@ubuntu:~/go$ ls
bin  pkg  src
```
You may ask, why this folders, well i used them for:
* **bin** is where all executables that we create are going to be stored.
* **pkg** is where dependencies packages will be stored
* **src** is where we will store our code 

After this steps we can go to the powerful **"Hello world!!"**
***********************************************************
##THe Mighty "Hello world!!"
I started by creating a directory inside `home/go/src` with my hello world project.

```bash
(base) ernestol@ubuntu:~$ cd go/
(base) ernestol@ubuntu:~/go$ ls
bin  pkg  src
(base) ernestol@ubuntu:~/go$ cd src/
(base) ernestol@ubuntu:~/go/src$ mkdir basic_golang
(base) ernestol@ubuntu:~/go/src$ cd basic_golang/
(base) ernestol@ubuntu:~/go/src/basic_golang$ pwd
/home/ernestol/go/src/basic_golang
```

Here we are going to start coding, it is recommended to name the file **main.go** but i think it is not mandatory. The file it should have an extension .go (This is MANDATORY).

After creating this file we are going to add the following code:
```go
package main

import "fmt"

func main() {
	fmt.Println("HELLO WORLD!!")
}
```

What looks nice to me that you do not event have to write the import statement, when you save the file, go automatically add you the import because it recognize that you are using the **fmt** and that you need to import it, this was really nice.

to execute it, you must first compile it (using `go build`):
```bash
(base) ernestol@ubuntu:~/go/src/basic_golang$ ls
src
(base) ernestol@ubuntu:~/go/src/basic_golang$ go build src/main.go
```

After this step you will notice a new file named **main** this is the one you will execute 
```bash
(base) ernestol@ubuntu:~/go/src/basic_golang$ ls -al 
total 1740
drwxrwxr-x 3 ernestol ernestol    4096 feb  1 15:16 .
drwxrwxr-x 3 ernestol ernestol    4096 feb  1 15:10 ..
-rwxrwxr-x 1 ernestol ernestol 1766430 feb  1 15:16 main
drwxrwxr-x 2 ernestol ernestol    4096 feb  1 15:13 src
(base) ernestol@ubuntu:~/go/src/basic_golang$ 


(base) ernestol@ubuntu:~/go/src/basic_golang$ ./main 
HELLO WORLD!!
(base) ernestol@ubuntu:~/go/src/basic_golang$ 
```

Another thing that looks very interesting to me was that if you write something that is not along with the syntax best practices, the VS Code plugin will let you know and go will not compile, let see this in action.

Let's say we want the code to appear nice by adding { in a new line:
```go
package main

import "fmt"

func main() 
{
	fmt.Println("HELLO WORLD!!")
}
```

VS Code will put it as an error:

![vs-code-erroring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5gos0z7gpe03r8absp5.png)

AN if you try to compile the code you will receive the following errors:
```bash
(base) ernestol@ubuntu:~/go/src/basic_golang$ go run src/main.go 
# command-line-arguments
src/main.go:5:6: missing function body
src/main.go:6:1: syntax error: unexpected semicolon or newline before {
```
SO you better pay attention to those errors,

Also you can notice that even if you add new lines **Go** will rearrange the code for you, **a nice feature if you ask me!**

FInally, if you want to run the code as a test without the need to compile it you can run: 
```bash
(base) ernestol@ubuntu:~/go/src/basic_golang$ go run src/main.go 
HELLO WORLD!!
```
**Note** take into account that this option provide less performance but it is a good way to test a change quickly.

**THIS WAS MY FIRST BABY STEP**
I will be posting more of this to document my journey on this language.

***********************************************************
## Sources:

* Go [Official website](https://go.dev/)
* GO [Blog](https://go.dev/blog/)
* **A cloud Guru**: [What is Go? An intro to Google’s Go programming language (aka Golang)](https://acloudguru.com/blog/engineering/what-is-go-an-intro-to-googles-go-programming-language-aka-golang)
* **PLatzi training**: [Curso Básico de Programación en Go](https://platzi.com/cursos/programacion-golang/)
* **TechWorld with Nana** [Golang Tutorial for Beginners | Full Go Course](https://www.youtube.com/watch?v=yyUHQIec83I)
* [Seven Golang Features you must know about](https://medium.com/@thisisdasc/seven-golang-features-you-must-know-about-944485d413fe)