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}

 

 

 

Certificate : Google’s Go Lang programming

Google’s recent programming language called as Go Lang is super awesome. Here is the certificated of completion for a Google’s Go Programming Language course by Todd Mcleod

golang-certificate

This certificate above verifies that Aseem Jain successfully completed the course Learn How To Code: Google’s Go (golang) Programming Language on Jan. 29, 2017 as taught by Todd McLeod on Udemy. The certificate indicates the entire course was completed as validated by the student.

GoLang : Type conversion VS type assertion in go

Just learned an interesting subtlety about Go. There are two ways to “cast” values. I put cast in quotes because one isn’t really casting but it’s just the general term I always use for saying “change on type to another one”.

The first one I was pretty used to, type conversion:

1
2
var x int = 5
var x64 int64 = int64(x)

The one I was unaware of is type assertion:

1
2
var x interface{} = int64(5)
var x64 int64 = x.(int64)

So what’s the difference? Well conversion should be used when you are dealing with a type, whether that be a constant or struct or whatever. Assertion is used when when you’re dealing with an interface. Say for example a method returns an interface instead of a struct, there’s still a value associated with the return but it has a generic type. You can use assertion to convert the return value to any type that implements that interface.

GoLang : Closure program

Hi Friends,

Here is a sample closure program. A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

package main

import "fmt"

func closureFunc() func() int {
       fmt.Println("init the x ")
       x := 1
       return func() int {
              x++
              return x
       }
}

func normalFunc() int {
       x := 100
       x++
       return x
}

func main() {
       increment := closureFunc()
       fmt.Println(increment())
       fmt.Println(increment())
       num := normalFunc()
       fmt.Println(num)
       increment1 := closureFunc()
       fmt.Println(increment1())
       fmt.Println(increment1())
       fmt.Println(increment())
}

/*
closure helps us limit the scope of variables used by multiple functions
without closure, for two or more funcs to have access to the same variable,
that variable would need to be package scope
*/

 

 

Go Lang : Setting up Go lang on Mac using brew

Install go by brew on Mac

Install golang by brew. I think that’s the easiest way to install go on OS X.

$ brew install go
==> Downloading https://downloads.sf.net/project/
machomebrew/Bottles/go-1.4.yosemite.bottle.ta
############################################# 100.0%
==> Pouring go-1.4.yosemite.bottle.tar.gz
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
  http://golang.org/doc/code.html#GOPATH

`go vet` and `go doc` are now part of the go.tools sub repo:
  http://golang.org/doc/go1.2#go_tools_godoc

To get `go vet` and `go doc` run:
  go get golang.org/x/tools/cmd/vet
  go get golang.org/x/tools/cmd/godoc

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
?  /usr/local/Cellar/go/1.4: 4557 files, 134M

Set Path for Go

Go needs paths for his root directory. I wrote like that.

# .zshrc
# go
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/.go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

It is actually religious problems how people set $GOPATH and $GOROOT. Let you set them up to your belief.

Run Tiny Program

To test, run tiny program of Hello World.

~/workspace
▶ vi hello.go
// hello.go
package main
import "fmt"

func main() {
  fmt.Printf("Hello, world!")
}
~/workspace
 go run hello.go
Hello, world!%

Now it’s working well!

Certificate : Go Lang programming

Go lang is a very efficient programing language by Google. I just finish initial programing course on Go lang.

go-lang-cert-UC-RHARR4X6.jpg

This certificate above verifies that Aseem Jain successfully completed the course Learn Google Go – Golang Programming for Beginners – Lite on June 10, 2016 as taught by EDUmobile Academy on Udemy. The certificate indicates the entire course was completed as validated by the student.

Certificate link : https://www.udemy.com/certificate/UC-RHARR4X6/