Go for Beginners — Part 2

In the previous Article (Which you can check out HERE), I laid the groundwork for me learning another language called Go.

In this Article I’m going to cover an Introduction to Go.

If you’re genuinely interested in this you can view all of my progress on my GitHub repo HERE

Lets get started…

An Introduction to Go

Compiling

When you write Go code, we’re writing it to be readable for developers. However it’s not readable by computers.

Go comes with a compiler which compiles our code to be able to be executed and turns it into a binary file similar to C.

To do this we go to terminal and type in

After this compiles, we will be given a new file called nameOfFile which is an executable binary file containing our translated Go code.

Running Our Code

Go also gives us the ability to automatically run our code after compiling with the command.

Packages

So now we are starting to take a deeper look into a Go programs structure.

Just like Python, Go’s compiler reads code from Top to Bottom.

However with it’s similarities to C every program in Go needs a mainfunction.

We also need to include the line package main as every Go program needs a package declarationto start off every file.

Importing Packages

Cool so now we need to import some basic packages for use to be able to do anything within Go. So lets import the fmt package for us to use.

Libraries are referred to as Packages within GoLang

We can do this with the keyword import and then the name of the package we want to import.

When importing Packages make sure to enclose them in double quotes "or else you'll get an error

Lets import the fmt Package to be able to use one of it's functions Println to be able to output text to the console

So far our code should look like this;

Functions

Functions are things we can call upon to run a specific piece of code when we want too.

Just like in Python, Functions can only be called after they have been declared. In layman’s terms, the Function needs to be higher up in the code then where you call it.

So lets create our first basic function within Go

To declare the Function we are going to use the keyword func followed by the name of the Function and two brackets (), then two curly brackets `{}

Hello World

Lets put this all together now,

Go uses Dot Syntax to refer to Methods / Functions

Then using go run main.go we get the output;

Boom! We’ve written our first program in Go

Importing Multiple Packages

If we want to import multiple Packages in go we can use a tiny bit of a different syntax to achieve this.

The great thing is this also allows us to create allias’ so you don’t have to type out the full Package name every time

This means we can refer to package1as package1Allias

We could change package1Alliasto anything such as a shorter way to remember the Package name such as `p1``

Comments

Comments in Go are similar to many other languages by using the keyword // this comments off the entire line after that keyword and it will not be read by the compiler

Summary

That is a basic introduction to the foundation of Go and it’s little quirks compared to other languages.

Backend Developer, Probably listening to music and drinking coffee