A Comprehensive Golang Tutorial

What Is GO?

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

It is a Free and Open Source programming language created at Google Inc. in 2007 by Ken Thompson (B, C, UNIX, UTF-8), Rob Pike(UNIX, UTF-8) and Robert Griesmer (HotSpot, JVM)

Go is created by leading tech giant Google. It is designed by Google to solve their problems of supporting scalability and effectiveness. Those are the issues you will face while creating your own servers.

Go (Go language) is Procedural, Imperative, Modular, Object-Oriented, Statically typed, Garbage Collected, Compiled programming language with syntax similar to a family of C languages (C, C++, C#, Java, JavaScript) & CSP style concurrency features. Programs are created using packages, for efficient management of dependencies. Go programming implementation use a traditional compile and link model to generate executable binaries. The Golang announced in 2009 and used in some of Google’s production system.

Go Program can be in length from 3 lines to millions of lines and it should be written into one or more files with the extension “.go”. For example, hello.go.

You can use “vi”,” vim” or any other text editor to write your go program into a file

Go Is Fast:

  • to learn
  • to develop
  • to compile
  • to deploy
  • to run

Bonus Advantages:

  • Golang has the speed of compiled language, but the feel of an interpreted language
  • Golang can make connections with thousands of users with the single instance only.

Valuable Resources Of Golang (By Todd McLeod):

Books On Golang:

Features Of Golang

1. Multicore Processing:

Most of the programming languages like Python, Java supports multithreading but the problem is with concurrent execution, thread- locking, race conditions and deadlocks. This makes it hard to create a multithreading application.

For example, when we are creating a new thread in java it will consume approx 1MB heap and for multiple thread creations, it will put tremendous pressure on heap and so will cause jvm not responding or shutdown.

So, Golang has developed by keeping this concurrency feature in mind. Go has goroutines instead of threads which consumes 2KB of memory from heap.

goroutines have faster startup than thread

Are You Looking For Golang Development Services?

2. MicroServices Support

Today all applications have no. of external services like databases, queues, caches. Go uses async I/O so that application can work with any no of services without blocking web request.

Other Features Include

  • Super performance
  • Clean Syntax
  • Garbage Collected
  • Concurrency
  • Powerful Standard Library
  • Open Source
  • Portable
  • Safe
  • It compiles fastly without makefiles, integrates with Git for distributed package management, and formats code with gofmt.

Go based system now runs on 400 servers,200+ million real-time connections & 10 billion messages daily.

Many big companies are using Golang for their projects

  • Google
  • DropBox
  • SoundCloud
  • Docker
  • Cloudflare
  • BBC

Installing Go:

To install Go, we need to go through the below steps.

Introduction:

For Windows, we need to use commands from Git Shell i.e. Terminal Emulator for distributed package management (optional). So we need Terminal Emulator for that.

3. Terminal Emulator-Git:

Download and install git for Windows to download go packages. We can also use the command prompt for the same. Now open it and test it using some basic terminal commands.

Installation Insights:

  1. Download Golang: You can download the Go lang binary release here. Download Golang distribution based on 32bit and 64bit OS
  2. Go workspace: Certain folder structure for Go development. Create a Project Folder having name where your project will reside

For example,

E:\golangdemo  (Name of my Workspace)
    bin folder  (binary exe file of project)
        pkg folder (.a Archive file à precompiled pkg file)
            src folder   (Source code files)
                github.com folder(downloaded dependencies)
                      golang.org folder (downloaded dependencies)

Search for go packages here

To download Go packages use following syntax –

→ go get <github_url>

for ex. > go get github.com/gorilla/mux (fully qualified pkg name)

To know more about go commands type > go in cmd prompt

Environment Path Variables-Windows

After creation of Go workspace set the Path variable of Go installation by following these steps –

Right Click on MyComputer/ThisPC→ Properties→ AdvancedSystem Settings → EnvironmentVariables→ New→

Here you need to set GOPATH and GOROOT for both User variables and System variable.

GOPATH→ Your workspace path

GOROOT→ Installed Path of GO

To check environment path variables use below command in cmd prompt –

→ env path variable stores the path to some location of computer

Development Environment

The most popular IDE’s (Integrated Development Environment) used for Golang development are Eclipse , WebStorm ,atom.

We are going to use Eclipse IDE here

  • Eclipse
  • GoClipse plugin
  • Creating new Go Project || Package || Library

How To Download And Install Goclipse Plugin To Your Eclipse –

In the Eclipse , right click on Help Menu→ Eclipse Marketplace→ search for ‘Goclipse’ and install it.

After the installation,

Go to File Menu→ Go Project→ Enter Project Name and choose default location as your workspace location

Your project structure will be like this –

Go to windows menu of eclipse→ select Preferences→ choose Go→ Tools→ Click on Download and it will download the dependencies like gocode, guru, godef, gofmt and click apply

Here, you just need to click on Download button for each of these paths.

While downloading check the console

Now, your Go installation and configuration is done.

Make sure you do not miss a single step.

For more information on Golang refer below links –

https://golang.org/doc/code.html

https://golang.org/doc/effective_go.html

As you are now all set with Golang, check out our next chapter on ‘Creating Your First Go Project

Content Team

This blog is from Mindbowser‘s content team – a group of individuals coming together to create pieces that you may like. If you have feedback, please drop us a message on contact@mindbowser.com

Keep Reading

Launch Faster with Low Cost: Master GTM with Pre-built Solutions in Our Webinar!

Register Today!
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?