Posts Tagged Under: golang

Golang – Organise your code using functions

Functions are a block of code that can call in your code. Functions and Packages are the 2 main ways used for organising code in Golang. You can feed parameters to functions and functions can also return values. You can call one function from inside another function.

Setup a Workspace for Go

Before you can start writing Golang code. You first need to set up a Workspace. A workspace is just a folder that will store all your Golang related stuff. In my case I’ll create a workspace called ‘go_project’:

$ mkdir /root/go_project

A workspace needs to have 3 top level folders called pkg, bin, and src:

$ mkdir /root/go_project/{bin,pkg,src}
$ tree /root/go_project
├── bin
├── pkg
└── src

Next, you need to tell Go to use this workspace. That’s done by setting an environment variable called ‘GOPATH’. Let’s first see what GOPATH is currently set to:

$ go env
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build207657744=/tmp/go-build -gno-record-gcc-switches"

I’m using bash, so I’ll set this using the .bash_profile approach.

You may want to have several workspaces for your various Go projects. If so, then you’ll need to

Installing Golang

Installing Golang is really easy, just following the instructions in the official guide. For example, for RHEL/CentOSm we would do something like:

curl -o ${golang_archive_filename}${golang_archive_filename}
tar -C /usr/local -xzf ${golang_archive_filename}
cd /usr/local/go/bin 
cp -rfp /usr/local/go/bin/* /usr/bin/

However here are a few other ways to install Golang:

RHEL/CentOS install tips

A quick way to install Golang is via yum:

yum install golang

But note that there’s a good chance the Go version yum installs could be at least several versions behind.

MacOS install tips

You can install golang using brew. With brew it’s as simple as:

$ brew install go

Then when new version of golang comes out, you can just do:

$ brew upgrade golang

Windows Install tips

You can install Golang using chocolatey:

c:\ choco install golang

Then when new version of golang comes out, you can just do:

c:\ choco upgrade															

Golang Tutorial

What is Golang?

Golang (aka G0) is a relatively new programming language Google that has taken the IT world by storm. It was released in 2009 and already some of the biggest applications are written in Go, including Docker, Terraform, and Kubernetes.

This is a series of bitesize tutorials that’ll help you get comfortable with using Golang. This course also requires you to have some experience with Linux, especially using the Bash terminal.

What makes Golang so great?

There’s a lot to like about Golang, here are some of the main reasons why Golang has become so popular:

  1. It’s a general purpose language – meaning that it can can be used for writing low level OS level components, which is usually done in C or C++. It’s also create for developing high level

Golang – A ‘hello world’ example

Once you’ve installed Golang and set up your workspace, you’re now ready to write your hello world go program. Your Go code has to be written in the src folder, and the file needs to have the ‘.go’ extension. So I created the following file:

$ cat hello.go
package main

// this is a comment. 
import "fmt"

func main() {
    fmt.Println("hello world")

There are 2 types of Go projects you can create. One is a shared library project which is code that’s used as part of other Golang projects. The other is where the end product of a Golang project is a standalone executable binary.

The package main tells Golang that this project will be creating a standalone executable binary. Next, we declared a function called ‘main’. You