Golang Cheat-sheet: Quick guide to start Golang for python developers.

Golang Cheat-sheet: Quick guide to start Golang for python developers.

As a cloud-native python developer in day-to-day infrastructure and Site reliability operations, I always wanted to learn Golang due to its great performance comparably and for its out-of-the-box simplicity in multiprocessing and multithreading.

Check out this blog post Go concurrency: https://opsinsights.dev/my-first-go-program

So, while getting started with Golang, I noticed similarities;

Otherwise, I tried to relate, and compare with python which helped me to grab the concepts, and syntax quicker. This is the motivation for this blog post, as understanding the similarities of what we already know helps our mind to easily adopt new learning.

Sharing my findings and understanding while learning GOlang. Part-1

A comprehensive guide to getting started with GO for python developers.

Highlights of Golang.

Go is expressive, concise, clean, and efficient\**

**as per documentation

  • Go is a compiled language. Statically typed.

  • Compiled executables are OS-specific, like a runtime. If compiled in windows will work only for windows.

  • Case sensitive

  • Go is designed for the next generation of C.

Sometimes it feels like python, but It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Advanced concepts in python like multiprocessing, and multithreading is day to day in Golang which is a core functionality.

Golang does not support.

  • Type inheritance

  • Method or Operator overloading

  • Does not have structured exception handling. (try catch as in python)

  • NO implicit numeric conversions

Importing packages.

.py

to import a package, below are some of the methods of importing modules, packages

import Game.Level.start #to import any specific module 
import Game.* #full import from Game.Level 
import start #without package prefix
import Game.Level as GL #alias imports

.go

import "fmt" import "os"

//to import multiple packages we can also enclose in simple brackets as shown below for.

import ( 
    "fmt" 
    "time" 
    "math" 
)

import mt "math" 
// syntax for alias import 

import . math 
// Dot import  allows to use modules without referencing package names.

Using variables

.py

variable declaration is a simple assignment using this operator. "="


variable_1 = 10 
variable_2 = "hello, team!"

*variables are case-sensitive.

.go

//var variableName dataType = initialValue
var x int = 10

there will be default initial values for variables if not assigned.

  • int and float types: 0

  • bool type: false

  • string type: ""

  • array and struct types: all of their elements are set to their respective zero values pointer, slice, map, channel, and function types: nil

For example, if you declare an integer variable x without an initial value, Go will assign it the zero value of int, which is 0:

Error handling

.py

try-catch block syntax in python.

try: 
    print(x) 
except: 
    print("An exception occurred")

.go

In Go, errors are represented as values of the built-in error, which can defined as.

type error interface { 
    Error() string 
}

If a function completes successfully the error value is null, else it will return the error.


The above example is with the interface, we will learn more in the next blog post.

All the above sections deserve an individual post, however*,* this will help you to kindle and kick-start Golang learning.


End of Part 1.

Happy learning. Thank you.

References:

https://go.dev/doc/code

https://blog.xojo.com/2017/12/06/compilers-101-overview-and-lexer/

Did you find this article valuable?

Support Jothimani Radhakrishnan by becoming a sponsor. Any amount is appreciated!