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/

 

Go Lang : Github link for go lang tutorial with exercises

Hey Friends,

https://github.com/ardanlabs/gotraining/tree/master/topics

This is an awesome link to learn go lang as it covers each topics, with actual code problem and solution and description.

It also has the link for Go play ground to try out your solutions

https://play.golang.org/p/i_2oDZ1ZSg

Enjoy Learning 🙂