How pointers work in GoLang

Pointers in Go Lang

Just like C, go lang also supported pointers. I have explained it in details with a simple working code.
Check out the code at github: https://github.com/premaseem/golangConcepts/blob/master/concepts/pointers/simplePointer.go

For details visit : http://www.premaseem.me

How Closures work in Go Lang

Closures in go

Closure is a nested function that has access to the parent variables, even after the parent has executed.

Github code :https://github.com/premaseem/golangConcepts/blob/master/concepts/closure/closure.go

For details visit: http://www.premaseem.me

Program to check website health using go lang

This program takes a list of website and checks the health status and reports whether website is up or down.
I have used go routines and go channels to explain it in detail.

Git hub code link: https://github.com/premaseem/golangConcepts/blob/master/miniProjects/websiteChecker/websiteChecker.go
For details visit : http://www.premaseem.me

Golang : Pass by Value Vs Pass by Refere

By default every thing in go is pass by value, which means if you pass any argument to a function, it would copy the values passed in another variable and make changes to it in function scope only (with in function block) and does not modify the original value.

Which means whatever you pass to a function in Golang remains immutable and does not get modified. However, if you use pointer then changes would impact the original value.

Try to read below program and understand it:

package main

import "fmt"

type Person struct {
       firstName string
       lastName  string
}

func changeName(p Person) {
       p.firstName = "Bad"
       fmt.Print("inside method with local scope on copy")
       fmt.Println(p)
}

func changeNameWithPointer(p *Person) {
       p.firstName = "Bad"
       fmt.Print("inside method with local scope on pointer")
       fmt.Println(p)
}

func main() {
       person := Person{
              firstName: "Good",
              lastName:  "person",
       }
       fmt.Println("Original value of person", person)
       changeName(person)
       fmt.Print("Outside method with original variable passed as copy")
       fmt.Println(person)

       fmt.Println("\n\n========Pass by pointer will modify values =======")

       fmt.Println("Outside method with original variable", person)
       changeNameWithPointer(&person)
       fmt.Println("Outside method with original variable passed as reference (which is now changed )")
       fmt.Println(person)
}

 

Sample output :

āžœ passByValue git:(master) āœ— go run ValueVsReference.go
Original value of person {Good person}
inside method with local scope on copy{Bad person}
Outside method with original variable passed as copy{Good person}
========Pass by pointer will modify values =======
Outside method with original variable {Good person}
inside method with local scope on pointer&{Bad person}
Outside method with original variable passed as reference (which is now changed )
{Bad person}