Golang Basics

mattia toselli
3 min readJul 18, 2022

Introduction

The language was conceptualized by Robert Griesemer, Rob Pike, and Ken Thompson.
Go uses and adjusts good ideas from various programming languages, while slightly avoiding features that led to complex, unstable, and unreliable code.
The tools of Go for concurrency are new and way more productive. Golang is particularly suited for developing infrastructure like networked servers, APIs, also tools and systems for developers.

Pros

  • Statically and strongly tiped, that leads to robust code, because variables should be defined at runtime.
  • Beautiful and active community.
  • A large number of libraries.
  • Simplicty of the language (it uses only 25 keywords).
  • Very fast compile time.
  • Very good garbage collector, and you can manage the memory by yourself if you need to.
  • Built in concurrency management.
  • Go compiles to a single binary, so that you that don’t have to wonder about dependencies, Dlls modules and stuff like that.

Hello world!

In order to start playing with Go you should go to this playground. Let us start with something simple:

Variables

Go has many ways to declare variables, below you’ll find the “longest” and more formal way:

As you can see, we used almost every type you could need in a basic application, the complete list of basic types in Go is:

bool

string

int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
// represents a Unicode code point

float32 float64

complex64 complex128

there’s a better way to declare variables, more concise, I am talking about this way:

Go will try to figure out the type needed for a variable (and is very good in doing that). Kind of a medium way is this one:

just after the declaration and the type, you can initialize the variable. Up to you which way you prefer to use.

The program below will raise two exceptions: one because j is not declared and one because i is created but not used, this is one of the features that i personally prefer, unused varables or functions are not allowed in Go, that leads to cleaner and lightwight code, and updates will roll more quicly in large applications.

Variables visibility

Golang has three levels of visibility for variables, and leverages on naming conventions.

  1. block scoped variables are variables declared inside a function, they will be accessible only inside that function.
  2. package scoped variable can be declared just after the import statements, if a variable defined here begins with a lowercase character then it will be accessible to every function of the module.
  3. globally scoped variables are defined like the package scoped variables, but if they start with an uppercase, they will be globally accessible.

Type casting

Variables can be transformed explicitly from a type to another one (not all conversions are possible, anyway).

Be sure not to lose informations when you perform a cast, if it is not your explicit purpose.

a special mention goes to string type casting, if we try to transform an integer or a float to a string, compile errors or odd results could occur, that’s why if you are trying to perform a cast, especially to string, you’ll need to check if you have to use some function, like in this case:

--

--

mattia toselli

Software developer at Fluentify LTD. AWS, Docker and programming enthusiast.