visit
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 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 : . 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 <filename.go>
Or compile it to make an executable file by : go build <filename.go> and then run it as an executable by ./<executable_file>
package main
import "fmt"
func main() { fmt.Println("Hello World !") }
Few Noteworthy points :
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
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 > <variable_name> <data_type> = <value>
syntax 2 : <variable_name> := <value>
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
append(array,7)
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 <function_name>(param_name param_type) < return_type>{ code }Syntax for fetching package : go install <package> or go get <package>
// 1st line is always the package
package main
// Importing basic packages in golang
import (
"fmt"
)
func main(){
add_of_number := sum(5,10)
square_of_number := square(5)
factorial_of_number := factorial(5)
fmt.Println(add_of_number,square_of_number,factorial_of_number)
print_n_numbers(3)
}
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; i<x+1; i++ { fmt.Println(i) }
}
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 (
"fmt"
"errors"
"math"
)
// struct type
type person struct {
name string
age int
height float64 }
func main(){
// Pointers ..
number := 10
fmt.Println(&number)
fmt.Println(number)
increment_number(&number)
fmt.Println(number)
// 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}
fmt.Println(p)
fmt.Println(p.name)
}
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++ }