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 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 file
Go is fast:
- to learn
- to develop
- to compile
- to deploy
- to run
- 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 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.
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
- Powerful Standard Library
- Open Source
- 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
To install Go, we need to go through below steps.
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 command prompt for the same. Now open it and test it using some basic terminal commands.
- Download Golang: You can download the Go lang binary release here. Download Golang distribution based on 32bit and 64bit OS
- Go workspace: Certain folder structure for Go development. Create a Project Folder having name where your project will reside
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
Most popular IDE’s (Integrated Development Environment) used for Golang development are Eclipse , WebStorm ,atom.
We are going to use Eclipse IDE here
- 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 –
As you are now all set with Golang, check out our next chapter on ‘Creating Your First Go Project‘