3 Steps To Learn Go: Moving From JavaScript and Python to Golang

Mastering a programming language takes years of time but learning a new language does not. And Trust me, Nobody is perfect when it comes to coding. As a software engineer, one should be at least familiar with minimum 4–5 different languages or at least he should know how to learn a new language in small amount of time.

Well, just to let you guys know, i recently switched from python and javascript to Golang. So, This post is just an attempt to guide developers on how to smoothly switch from one language to another.  

Note : [ This post is for people who have prior experience of any programming language and wants to learn practical Go programming concepts in a quick and easy way (just by looking at Go code !) ]

Step 1 : Why does the language exist and what are its advantages 

Golang is a very powerful and popular programming language in the software industry. Golang is expressive , statically typed, compiled language, is fast and reliable and a lot better than JavaScript and Python in many ways. It is slowly gaining momentum and is becoming the first language of choice among many developers for developing software’s, modern web apps and app backend.

It is used to make a large scale and reliable apps that involves concurrency. The only con of using go is that their are very few number of resources on the internet to learn or to do/complete tasks and develop software. 

Step 2 : Setup, Installation & running the “Hello world” Program . 

In order to learn a new language one needs to download the language (binaries /SDK) itself. So, simply download golang by clicking on the link below.

Link : https://golang.org/dl/. Set up a workspace and create a basic go file in the text editor, copy and paste the below code and run the file . 

Directly run the file by : go run  

Or compile it to make an executable file by : go build and then run it as an executable by ./

package main
import "fmt"
func main() { fmt.Println("Hello World !") }

If the code outputs Hello World ! You are good to go 🙂 

Few Noteworthy points : 

  1. Every file that we need to execute must have a main package at the top. 
  2. Next comes the import statement for reading and writing. 
  3. Next is the main function from where the program starts. 

Step 3 : Basics of Programming, Syntax and Coding Patterns.  

When it comes to programming, basics remains the same. No matter what language we are learning there will always be data types, if else statements, loops, switch statements etc …. Lets look at the basics of Golang.

Here, i will be discussing about : 

  • Data Types 
  • Control Statements 
  • Functions (defining and declarations)
  • Loops 
  • Pointers
  • Error Handling 
  • Packages (creating and importing)
  • creating data structures 

Lets look at them one by one. 

Data Types 

Like all other languages, Golang also supports a lot of similar primitive datatypes like int , float64, strings ,bool etc … let’s look at the syntax : 

syntax 1 : < var > =

syntax 2 :  :=

var x int 
fmt.Println(x) // output 0 or 
x := 5 
fmt.Println(x) // output 5
name := "Karan"
fmt.Println(name) // output karan
var array []int{1,2,3,4,5,6}
fmt.Println(array) // output [1,2,3,4,5]
fmt.Println(array[2]) // output 3 
fmt.Println(array) // output [1,2,3,4,5,7]

Control Statements, Functions, Importing packages  

Let’s look at the basics of control flow statements like ( if else, switch, loops ) , function declarations and function calling in Golang with the help of an example.

Syntax of a function can be seen below.  

func (param_name param_type) < return_type>{ code }

Syntax for fetching package : go install or go get

// 1st line is always the package 
package main 
// Importing basic packages in golang 
import (
func main(){
  add_of_number := sum(5,10)
  square_of_number := square(5)
  factorial_of_number := factorial(5)
func sum(x int , y int) int { return x + y }
func square(x int) int { return x*x }
func factorial(x int) int {
 if x < 1 { return 1 }
  return x*factorial(x-1)
func print_n_numbers(x int) {
  for i:=1; i1; i++ { fmt.Println(i) }

// output //

15 25 120

1 2 3

Error Handling, Pointers and Struct types  

Interesting fact : Go doesn’t have Exceptions so we have to check errors manually and also a function can return multiple values. Golang struct are used to combine multiple types of data into one. It can also be used to create a new data structure. Lets look at an example. 

// 1st line is always the package 
package main
// Importing basic packages in golang 
import (
// struct type 
type person struct {
name string
age int
height float64 }
func main(){
// Pointers .. 
number := 10
// Error Handling .. 
sqrt_result , err := sqrt(16)
if err !=nil{ fmt.Println(err) } else { fmt.Println(sqrt_result) }
// accessing struct in golang .. 
p := person{name:"karan",age:25,height:5.10}
func sqrt(x float64) (float64 , error){
if x< 0 { return 0 , errors.New("Negative number is not allowed !") }   else {  return math.Sqrt(x), nil } }
func increment_number(x *int){ *x++ }

// output //

0Xc82000a2f0 10 11 4 {karan 25 5.1} karan

That’s it for this post. I hope you liked it. Till then, Keep coding and keep learning. 

read original article here