Let’s Go:: A very brif introduction to Go language

 

Hello World I am back! I know it took *a bit* long resuming this blog as I got pretty busy in other stuff related to my work.

Alright so this post is about writing applications in Go, a programming language create at Google by Ken Thompson(“K” from famous K&R) and the creator of Unix and Rob Pike.

I had not thought of learning another programming but there a few reasons I go for the Go

  • Go is a minimal language like Python. Since it was developed by Googlers who are also Python lovers, they came up a language which is simple to write like Python but more efficient like C++ so Go is kind of cusp of the two thus helps new learns to write efficient programs at Google.
  • Unlike Python/PHP it’s a compiled language, your code is built and then available to run anywhere just like C programs.
  • Go provides better features to write concurrent programs. In the era when multi-core apps are being written, Go addresses the need quite well and there’s built-in support of concurrency in it.
  • Go is *Not* an Object Oriented language so another reason to try something different.
  • Last but not least, I wanted to learn it!

This picture sums up where Go is standing

Image Credit: Keval Patel

Go is not an Unknown language anymore, some good and famous projects are using it, for instance kubernetes and Ethereum Crypto Project are based on Golang.

Let’s dig in!

Installation and Setup

Go to Golang’s official site download section and grab the binary based on your OS. To check whether it’s installed correctly, go to the terminal and run the following command:

go version

For me it prints

go version go1.9 darwin/amd64

which is the latest version as of this post was being written.

Now before you start coding,  you also need to make sure certain PATH variable is set. You should set  GOPATH, something similar to JAVA_HOME. Once you are done you can verify it by running the command:

go env

and if it’s all set your screen should look like this:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Development/PetProjects/GoProjects"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d3/t5c58nx570ggzzknhm4zpjnw0000gn/T/go-build775007936=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

Development

I am fan of jetbrains’ IDEs. I am using Gogland, the Golang IDE being offered by them but you are not supposed to use it anyway. You can use any text editor to write code and then command line go tool to compile and run your programs.

After everything is setup, it’s time to write the first program. I created a file with name intro.go

package main

import "fmt"

func main() {
	fmt.Println("Hello, Guys")
}

So this is the simplest flavour I could come up.

Go applications are comprised of packages and main is the package that is always needed. If you run without it, you will get the following error:

➜  GoProjects go run intro.go
package main: 
intro.go:2:1: expected 'package', found 'import'
➜  GoProjects 

The next line is importing the libraries you want to use in your programs. If you are coming from Java or Python background then this should not be something new for you.  In this case I am using fmt package which is used for I/O formatting and other print/input related things. Learn more about fmt here.

Then.. there is a main function. Again it is not something alien for C and Java programmers. The next line is printing the text on console.

OK, so now build and run it. Go to the terminal and run

go build intro.go

It will generate an executable file in the same directory. If you run the command go install then it will generate package with all dependencies for using on other machine. Make sure you set GOBIN path for that purpose.

Let’s change program a bit to discuss further things:

package main

import (
	"fmt"
)

func info(name string, occupation string) (string, string) {
	final_message := "So " + name + ", your occupation is " + occupation
	another_return := "LOL!!"
	return final_message, another_return
}

func main() {
	var user_name, user_occupation string
	user_name = "Adnan"
	user_occupation = "Programmer"
	result, result2 := info(user_name, user_occupation)
	fmt.Println(result, result2)
}

 

Variables

You use var when declaring variables, Here user_name and user_occupation .You also specify the type of variable. In my case it’s string.

There is also a short method of declaration, := . When you do that you don’t specify the type as it gets the type from the statement right side of it. Also you don’t declare these variables above otherwise you get the error:
no new variables on left side of :=

Variables without any value are declared as Zero, Zero value is:

  • 0 for Numeric
  • false for boolean
  • "" for string

Functions

I created a simple function info it takes two string parameter. Like Parameter you also need to mention returned type which is string in my case. In Go a function can return multiple values.

Loops

In Go only for loops is available which you can use for a typical for loop and while loop.

Typical for loop

for i:=0; i < 10 ; i++ {
		fmt.Println(i)
	}

Short Form

i := 0
	for ; i < 10; {
		fmt.Println(i)
		i++
	}

While loop

for i < 10 {
		fmt.Println(i)
		i++
	}

Forever loop

for {
	fmt.Println("Forever...")
    }

That’s it for now. These were some features which you may found common in other languages but dealt differently. In future Go related posts I will discuss some advance topics.

 

If you like this post then you should subscribe to my blog for future updates.

* indicates required



One Comment