DEV Community

Cover image for Learning GO (Golang): First steps and the mighty "Hello world"
Ernesto Lopez
Ernesto Lopez

Posted on

Learning GO (Golang): First steps and the mighty "Hello world"

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

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

According to GO official website you can use Go for:

Use-cases-for-go
Source:GO 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, Kubernetes and Prometheus 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
Source:Who is using GO

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

After the software was downloaded i follow this steps:
GO Installation guide

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
Enter fullscreen mode Exit fullscreen mode

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)

(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
Enter fullscreen mode Exit fullscreen mode

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.

(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
Enter fullscreen mode Exit fullscreen mode

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:

package main

import "fmt"

func main() {
    fmt.Println("HELLO WORLD!!")
}
Enter fullscreen mode Exit fullscreen mode

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):

(base) ernestol@ubuntu:~/go/src/basic_golang$ ls
src
(base) ernestol@ubuntu:~/go/src/basic_golang$ go build src/main.go
Enter fullscreen mode Exit fullscreen mode

After this step you will notice a new file named main this is the one you will execute

(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$ 
Enter fullscreen mode Exit fullscreen mode

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:

package main

import "fmt"

func main() 
{
    fmt.Println("HELLO WORLD!!")
}
Enter fullscreen mode Exit fullscreen mode

VS Code will put it as an error:

vs-code-erroring

AN if you try to compile the code you will receive the following errors:

(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 {
Enter fullscreen mode Exit fullscreen mode

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:

(base) ernestol@ubuntu:~/go/src/basic_golang$ go run src/main.go 
HELLO WORLD!!
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (2)

Collapse
 
jamesli2021 profile image
jamesli2021

Not useful, there is so many hello world and tutorials. My advice is spend your time doing something useful instead.

Collapse
 
aernesto24 profile image
Ernesto Lopez

Totally understood, and thanks for the comment, but as i wrote at the beginning i am just documenting my journey on this new language, and every journey has a beginning.