Golang - Getting Started

codehakase

Francis Sunday

Posted on November 18, 2017

Golang - Getting Started

“Go will be the server language of the future.” — Tobias Lütke, Shopify

Go was first created as an experiment, the goal of its creators was to come up with a language that would resolve bad practices of others while keeping the good things.

Its first release was on March 2012. Go was designed to feel familiar and to stay as simple as possible, the entire language specification fits in just a few pages.

What's this series about?

This series is a gentle introduction to the Go programing language, covering the basic concepts, syntax and features of Go. The ending parts of the series would focus on building from Zero to Live app using the Go programming language.

Why Go?

Why would you choose Golang over tonnes of other languages such as python, ruby, nodejs, etc?

Here are some of the pros I've seen in Go:

  • Concurrency is an inherent part of the language. As a result writing multi-threaded programs is a piece of cake. This is achieved by Goroutines and channels (will be discussed fully in next parts of this series).

  • Golang is a compiled language. The source code is compiled to native binary. This is missing in interpreted languages such as JavaScript used in nodejs.

  • The language spec is pretty simple. The entire spec fits in a page and you can even use it to create your own compiler... sweet isn't it?

Installation && Setup

Golang is supported on all the three platforms Mac, Windows and Linux. You can download the binary for the corresponding platform here - https://golang.org/dl .

Linux setup

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go.

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Enter fullscreen mode Exit fullscreen mode

Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/usr/local/go/bin
Enter fullscreen mode Exit fullscreen mode

Mac OS X package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.

The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

Windows installation

Open the MSI file and follow the prompts to install the Go tools. By default, the installer puts the Go distribution in c:\Go.

The installer should put the c:\Go\bin directory in your PATH environment variable. You may need to restart any open command prompts for the change to take effect.

Testing our Installations

To test if Go is properly installed on our machine, we need to create a workspace, and write our first program.

We'll first create our workspace:

cd $HOME/go && mkdir test
Enter fullscreen mode Exit fullscreen mode

Next we create our source file test.go with the following content:


package main 

import "fmt"

func main() {

  fmt.Println("Hello World!")
}
Enter fullscreen mode Exit fullscreen mode

The code above, represents a simple Go program. Every Go program is made up of packages. Our program, starts running in the package main. This program is using the package with import paths "fmt".

By convention, the package name is the same as the last element of the import path. For instance, the fmt package comprises files that begin with the statement package fmt.

To build the program, Go provides a build tool for us to use:

cd $HOME/go/test

go build test.go
Enter fullscreen mode Exit fullscreen mode

The command above will build an executable named test in the directory alongside the source code. Execute it to see the greeting:

./test
Hello World!
Enter fullscreen mode Exit fullscreen mode

If you see the greeting, you've been successful so far, and your Go installation works.

This is the first article in the Golang tutorial series. In the next articles, I'll be covering the following topics:

  • Variables - VIEW
  • Types - VIEW
  • Constants - VIEW
  • Functions - VIEW
  • Packages
  • Conditional Statements and Loops
  • Arrays, Slices and Variadic Functions
  • Pointers, Structures and Methods
  • Interfaces
  • Concurrency
  • Object Oriented Programming
  • Defer and Error Handling

I hope you've enjoyed this part of the series? You can read other articles by me on my Blog or on The Practical Dev.

If I've missed anything, let me know in the comments.

💖 💪 🙅 🚩
codehakase
Francis Sunday

Posted on November 18, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Golang - Getting Started
go Golang - Getting Started

November 18, 2017