99devops

99devops

A highly trained monkey’s blog

09 Oct 2021

Learning Go ? Start here !!! - Day 0

go

After playing with go for a while, i have decided that i will start to write some blog about go and some information which you need to know to get started on learning go lang. At least what i followed to knowing go to effectively start writing your own application or api.

First of all, let’s do a small theory on what is go and why go after all you would need to know why you choose to work with go and not with other programming language like python, java etc

What is Go ?

Go is a statically typed, compiled programming language which was created at Google around 2007-2009. Go syntax are some what inspired from C with whole lot more features like garbage collection, concurrency etc.

Why use Go ?

Programming is like juggling. You would need to know how to juggle between multiple programming language based on the work you are doing. Go was built by legends who has worked in the same industry for decades and have seen the issues with past and modern programming language. The main reason to choose Go over other language like python are:

  1. Has built-int garbage collectors which prevents memory leaks and proper memory management.

  2. Unlike C, C++ or even JAVA, sytanx of Go is very easy to read, write and understand. This was intentional. Creators of Go ( Ken thomson and team) wanted to make programming effecient but also want to keep it simple.

  3. One thing which fascinated me was Go does not use while loop, but instead for look can be used to perform this task.

  4. Error handling is also another important factor which i like about go. Comparing to other programming language, there are need to handle errors with exceptions but error handling is done as part of go code itself and is easier to implement.

  5. Speed and Performance is another contributing factor. If you want your application to be very fast and give high performance then it is better to develop application in Go. As we know, running pre-built and compiled binary is faster than running through application directly from source code. Some more info on performance

  6. Concurreny and Channels is another feature that makes go first class choice for creating concurrent application. Go uses something called goroutines and channe to help create concurrent applications.

  7. Source code formatting code formatting is also another feature which is very neat. Gone are the days where you go about formatting your code for better readability.

  8. Go is opionated programming language. It follows good type system and It also throws error when unused variable and libraries exists in application.

  9. Easy packaging and compilation of application based on target OS and Architecture.

Prerequisite

To be honest, there are not any pre-requisite of learning go. You can use go as your first programming language and still be okay with it. If you have some prior programming experience with language like C, Java, Python then it will be easier to understand.

When to use Go ?

There are always tradeoff in any language you choose and go is no different. It sure has some disadvantages, it is time consuming compared to writing code in python, it is still young, there are not much frameworks built. It is up to developer to acknolwdge these limitations and know what language is better for your application.

Go is better suited for creating backend applications / apis where performance, memory footprint, processing time, concurrency matters.

Dissecting a simple hello world go app

First of all, all go codes are created in a file called *.go. In go, there should be an entrypoint of the application and is generally defined in file called main.go but you can name it any filename. main.go is the main entrypoint into the application.

Let’s look at a sample Hello world code written in go

package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}

Let’s have a closer look at the code

package main : Go has an idomatic approach where program execution starts. It first looks for main function inside main package to start the execution. Go application must have a main package which acts as an entry point to your application. This directive means that this file is a main file for the go application.

import "fmt" : This step is importing fmt package which consists of methods to stdout information using Println or Printf and more.

func main() {} : This step tells we are creating a function named main. As discussed each go should have a main package and this main function is the entrypoint of that app. Go looks for main function as an entrypoint function.This is similar to creating main function while using JAVA applications.

fmt.Println("Hello world") : This step tells that we want to use Println method from fmt package. Println formats the input with the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended.

Now, we know write a simple go application, let’s try to run it. You can run go programs direcly like this

% go run main.go
Hello world

or, you can compile the code into a binary executable and run from it. This method is much faster. This will create an executable main and you can execute it like how you run other executable.

% go build main.go
% ./main

You can use go online editor to code and run you code Here

Conclusion

Go is very good for creating backend application for web servers and for creating really fast utilities. Go is young but with that is promised right now, we know for sure that Go is here to stay.

comments powered by Disqus