Book Image

Go Design Patterns

By : Mario Castro Contreras
Book Image

Go Design Patterns

By: Mario Castro Contreras

Overview of this book

Go is a multi-paradigm programming language that has built-in facilities to create concurrent applications. Design patterns allow developers to efficiently address common problems faced during developing applications. Go Design Patterns will provide readers with a reference point to software design patterns and CSP concurrency design patterns to help them build applications in a more idiomatic, robust, and convenient way in Go. The book starts with a brief introduction to Go programming essentials and quickly moves on to explain the idea behind the creation of design patterns and how they appeared in the 90’s as a common "language" between developers to solve common tasks in object-oriented programming languages. You will then learn how to apply the 23 Gang of Four (GoF) design patterns in Go and also learn about CSP concurrency patterns, the "killer feature" in Go that has helped Google develop software to maintain thousands of servers. With all of this the book will enable you to understand and apply design patterns in an idiomatic way that will produce concise, readable, and maintainable software.
Table of Contents (17 chapters)
Go Design Patterns
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Installing Go


Any Go Installation needs two basic things: the binaries of the language somewhere on your disk and a GOPATH path in your system where your projects and the projects that you download from other people will be stored.

In the following lines, we will explore how to install Go binaries in Linux, Windows and OS X. For a detailed explanation of how to install the latest version of Go, you can refer to the official documentation at https://golang.org/doc/install.

Linux

To install Go in Linux you have two options:

  • Easy option: Use your distribution package manager:

    • RHEL/Fedora/Centos users with YUM/DNF: sudo yum install -y golang

    • Ubuntu/Debian users using APT with: sudo apt-get install -y golang

  • Advanced: Downloading the latest distribution from https://golang.org.

I recommend using the second and downloading a distribution. Go's updates maintains backward compatibility and you usually should not be worried about updating your Go binaries frequently.

Go Linux advanced installation

The advanced installation of Go in Linux requires you to download the binaries from golang webpage. After entering https://golang.org , click the Download Go button (usually at the right) some Featured Downloads option is available for each distribution. Select Linux distribution to download the latest stable version.

Note

At https://golang.org you can also download beta versions of the language.

Let's say we have saved the tar.gz file in Downloads folder so let's extract it and move it to a different path. By convention, Go binaries are usually placed in /usr/local/go directory:

tar -zxvf go*.*.*.linux-amd64.tar.gz
sudo mv go /usr/local/go

On extraction remember to replace asterisks (*) with the version you have downloaded.

Now we have our Go installation in/usr/local/go path so now we have to add the bin subfolder to our PATH and the bin folder within our GOPATH.

mkdir -p $HOME/go/bin

With -p we are telling bash to create all directories that are necessary. Now we need to append bin folder paths to our PATH, append the following lines at the end of your ~/.bashrc:

export PATH=$PATH:/usr/local/go/bin

Check that our go/bin directory is available:

$ go version
Go version go1.6.2 linux/amd64

Windows

To install Go in Windows, you will need administrator privileges. Open your favorite browser and navigate to https://golang.org . Once there click the Download Go button and select Microsoft Windows distribution. A *.msi file will start downloading.

Execute the MSI installer by double clicking it. An installer will appear asking you to accept the End User License Agreement (EULA) and select a target folder for your installation. We will continue with the default path that in my case was C:\Go.

Once the installation is finished you will have to add the binary Go folder, located in C:\Go\bin to your Path. For this, you must go to Control Panel and select System option. Once in System, select the Advanced tab and click the Environment variables button. Here you'll find a window with variables for your current user and system variables. In system variables, you'll find the Path variable. Click it and click the Edit  button to open a text box. You can add your path by adding ;C:\Go/bin at the end of the current line (note the semicolon at the beginning of the path). In recent Windows versions (Windows 10) you will have a manager to add variables easily.

Mac OS X

In Mac OS X the installation process is very similar to Linux. Open your favorite browser and navigate to https://golang.org and click the Download Go. From the list of possible distributions that appear, select Apple OS X. This will download a *.pkg file to your download folder.

A window will guide you through the installation process where you have to type your administrator password so that it can put Go binary files in /usr/local/go/bin folder with the proper permissions. Now, open Terminal to test the installation by typing this on it:

$ go version
Go version go1.6.2 darwin/amd64

If you see the installed version, everything was fine. If it doesn't work check that you have followed correctly every step or refer to the documentation at https://golang.org.

Setting the workspace - Linux and Apple OS X

Go will always work under the same workspace. This helps the compiler to find packages and libraries that you could be using. This workspace is commonly called GOPATH.

GOPATH has a very important role in your working environment while developing Go software. When you import a library in your code it will search for this library in your $GOPATH/src. The same when you install some Go apps, binaries will be stored in $GOPATH/bin.

At the same, all your source code must be stored in a valid route within $GOPATH/src folder. For example, I store my projects in GitHub and my username is Sayden so, for a project called minimal-mesos-go-framework I will have this folder structure like $GOPATH/src/github.com/sayden/minimal-mesos-go-framework which reflects the URI where this repo is stored at GitHub:

mkdir -p $HOME/go

The $HOME/go path is going to be the destination of our $GOPATH. We have to set an environment variable with our $GOPATH pointing to this folder. To set the environment variable, open again the file $HOME/.bashrc with your favorite text editor and add the following line at the end of it:

export GOPATH=${HOME}/go

Save the file and open a new terminal. To check that everything is working, just write an echo to the $GOPATH variable like this:

echo $GOPATH
/home/mcastro/go

If the output of the preceding command points to your chosen Go path, everything is correct and you can continue to write your first program.