quapona technologies
The Go Gopher

In the introduction of our Go series, we have briefly presented the reasons choosing Go for our new project. The final product we want to create is a distributed server architecture which will provide several services. According to the “batteries included” slogan of the Go developers, it already comes with most of the necessary tools and libraries implemented, tested and ready to use. We will propose a solution how we implement a service-based server structure, which is easy to maintain, support short implementation cycles and quick deployment.
This part of the Go series is about how to prepare the machine and how we organize the code. Thereby, we will see how to install Go on a system and how to configure the environment to get started with our first program.

Setting Things Up

To set up the machine we need an operating system (OS) which fits the needs to be reliable, well maintained and which won’t get abandoned during the development and maintenance phase. Using the same OS for the development and the server machines seemed to be a good idea to reduce the cost and learning curve for the administration of the remote servers. Our decision has been made and we will use Debian as the OS running all the Go code. Despite our choice, it is possible to use any of the available and go supported OSs out there as well. But all of our How-tos and explanations will be focus on the use of Go on a Debian-based machine.

Fortunately an official installation manual for Debian can be found on the official webpage, so we can skip this step here and assume the system is running and ready to configure.

Open a terminal and follow the steps to install Go on your machine.

First of all, we need to update the package sources

:$ [sudo] apt-get update 

and install the go package

:$ [sudo] apt-get install golang 

The installation of the golang package will configure the system to work with the tools Go is bringing along. After the installation is done, test if everything went well executing the "go env" tool. It prints the environment variables used by Go and you should see a similar output like this

GOARCH=”amd64”
GOBIN=””
…
GOPATH=””
…
CXX=”g++”
CGO_ENABLED=”1” 

The, at the moment, most important environment variables are the GOPATH variable, which tells Go where to look for source folders (named src) and precompiled packages (called pkg) to use for compilation, and the GOBIN path, which is used by the install command to write the binary to. To set the paths properly, we first have to create a workspace folder to store our code in. Just use the following commands

:$ mkdir ~/golang
:$ cd ~/golang 

to create a folder named golang in your home directory and change your working directory to it. In this new workspace folder, we create two new directories called bin and src with the same command as above:

:$ mkdir bin
:$ mkdir src 

The bin directory will be used for the GOBIN variable. In the src folder the Go source code will reside respectively. Next, we need to set the GOPATH and GOBIN variables with

:$ export GOPATH=$HOME/golang
:$ export GOBIN=$GOPATH/bin 

This will store the path in the environment variables for this session only.

Note: You have to use different directory paths, if you have created the directories somewhere else)

Additionally, it is recommended to add the GOBIN path to the PATH variable to ease the execution of installed go programs with

:$ export PATH=\$PATH:\$GOBIN 

To export the paths persistently between two sessions, you can append the export commands to the .bashrc file, which is located in your home directory (if you are using a shell other then bash, you may have to use another file)

:$ echo “export GOPATH=\$HOME/golang” >> ~/.bashrc
:$ echo “export GOBIN=\$GOPATH/bin” >> ~/.bashrc
:$ echo “export PATH=\$PATH:\$GOBIN” >> ~/.bashrc 

to execute the commands at login time.
If you are using more than one workspace, it is a best practice to set the environment variables for every workspace each. For example, if you have workspaceOne, workspaceTwo and workspaceThree in your home directory you can set

:$ export WSONE=$HOME/workspaceOne
:$ export WSTWO=$HOME/workspaceTwo
:$ export WSTHREE=$HOME/workspaceThree 
:$ export WSONEBIN=$WSONE/bin
:$ export WSTWOBIN=$WSTWO/bin
:$ export WSTHREEBIN=$WSTHREE/bin 

and add them to the GOPATH and GOBIN with

:$ export GOPATH=$WSONE
:$ export GOPATH=$GOPATH:$WSTWO
:$ export GOPATH=$GOPATH:$WSTHREE 
:$ export GOBIN =$WSONEBIN
:$ export GOBIN =$GOBIN:$WSTWOBIN
:$ export GOBIN =$GOBIN:$WSTHREEBIN 

Using this approach, you can add the GOBIN paths to the PATH variable as shown above.

Getting Things Organized

Many Tutorials out there do a very good job in showing you the essentials to start from scratch coding Go (like the official Tour of Go). But they all lack explanations in preparing you, setting up your workspace in the right form.
As the quapona®technologies development team may publish their sources in the future, we recommend everyone to start with a fully qualified folder name for all packages like

golang/src/sub.domain.com/project_name/repository 

If you are using git to versioning your code, using GitHub or GitLab, you can use the folder structure

golang/src/github.com/name/repository 

to pull and push them from and to Github.
OK, what’s the deal with the path, anyway? Go comes with a variety of tools to use for development, testing, formatting etc. One of these tools is the “go get” tool. Using the “go get” command, go automatically pulls the packages you are importing in your code from the sources of each import path. These pulled packages will be then placed at a path like

<workspace_dir>/src/<import_path> 

Comparing the pattern above with the suggested folder structure, it’s clear from which address the “go get” command will pull the sources.

Getting Things Started

The last chapter in this article starts with coding. At first we need a file to write our code into. Therefore, we change the working directory in the terminal to our golang workspace with

 :$ cd $GOPATH/src 

In this directory we create a new directory path and change our working directory.

:$ mkdir –p my.domain.com/awesomeuser/firstprog
:$ cd my.domain.com/awesomeuser/firstprog 

There we will create a new file called main.go

:$ touch main.go 

Use an editor of your choice to add some code to the file.
We will start to add the basic code structure for a standalone Go program.

package main
import “fmt”
func main() {
    fmt.Println(“You are an awesome programmer!”)
} 

Save the file and run the build command in the terminal.

:$ go build main.go 

If there is no output in the terminal, then the build succeeds without any error. Now run the code with

:$ main 
You are an awesome programmer! 

The output should be the same as above. Congratulations, you have compiled your first go program. Now we will use the install command to copy the output of the build command to the GOBIN path.

:$ go install main.go 

You should be able to execute your program from anywhere on the computer by using

:$ cd ~
:$ main
You are an awesome programmer!
:$ type main
main is hashed (/home/user/golang/bin/main) 

Note that this only works if you added the GOBIN path to the PATH variable of the system. Otherwise you have to change your working directory into the GOBIN to execute your program. The type command at the end shows that the main is executed from the GOBIN directory.

Knowing that this is yet another “Hello World” example, we soon will provide more advanced topics for writing Go code. In the next articles we will have a look at build-in tools, including the build tool and how it is working.

If you like this article, let us know in the comments area below.
Feel free to ask any question or give us feedback.

The quapona® technologies Development Team

Kommentare powered by CComment

Beitrag teilen