Exporting name

To export a name in Go, just make it’s first letter an uppercase letter. From within a package namespace you can refer to private functions and variables (starting with lower case) but from outside the package, you can only access the things exported from that package. Think public and private key word in Java.

//exported 

func MyPublicFunc(){

}

//private 
func myPrivateFunc(){

}

Go - defer

The defer statement is used to postpone a function call executed immediately before the surrounding function returns. The common uses of defer include releasing resources (i.e., unlock the mutex, close file handle.), do some tracing(i.e., record the running time of function), etc. E.g., an ordinary accessing global variable exclusively is like this: var mu sync.Mutex var m = make(map[string]int) func lookup(key string) int { mu.Lock() v := m[key] mu.Unlock() return v } An equivalent but concise format using defer is as follow: [Read More]

Go - literals

literal means the value itself. Unlike variable, a literal doesn’t have a name.

Go functions

A function can take zero or more typed arguments. The type comes after the variable name. Functions can be defined to return any number of values that are always typed. package main import "fmt" func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) } Go Tour Functions example In the following example, instead of declaring the type of each parameter, we only declare one type that applies to both. [Read More]

Scopes in Go

Same name cannot be declared again inside a same scope. There are following types of scopes in Go:

  1. package: Each Go package has it’s own scope. For e.g. declared funcs are only visible to the files belonging to same package.
  2. file: Imported packages are only visible to the importing file. Each file has to import external packages on it’s own.
  3. func.
  4. block.