Detect Data Race

“Data race” is a common but notorious issue in concurrency programs. sometimes it is difficult to debug and reproduce, especially in some big system, so this will make people very frustrated. Thankfully, the Go toolchain provides a race detector (now only works on amd64 platform.) which can help us quickly spot and fix this kind of issue, and this can save our time even lives! Take the following classic “data race” program as an example: [Read More]

Installing some common tools

Use go get to download packages and their dependencies, then install them: go get github.com/golang/lint/golint \ golang.org/x/tools/cmd/goimports Golint looks for common code style mistakes. Golint makes suggestions for many of the mechanically checkable items listed in Effective Go and the CodeReviewComments wiki page. goimports formats your code correctly (it’s a drop-in replacement for gofmt) and also removes any unused imports. It will also try to resolve imports that haven’t been added to the imports definition. [Read More]

Need not close every channel

You don’t need to close channel after using it, and it can be recycled automatically by the garbage collector. The following quote is from The Go Programming Language: You needn’t close every channel when you’ve finished with it. It’s only necessary to close a channel when it is important to tell the receiving goroutines that all data have been sent. A channel that the garbage collector determines to be unreachable will have its resources reclaimed whether or not it is closed. [Read More]

nil channel VS closed channel

The zero value of channel type is nil, and the send and receive operations on a nil channel will always block. Check the following example: package main import "fmt" func main() { var ch chan int go func(c chan int) { for v := range c { fmt.Println(v) } }(ch) ch <- 1 } The running result is like this: fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send (nil chan)]: main. [Read More]

Setting up gRPC

The example code for our tutorial is in grpc/grpc-go/examples/route_guide. To download the example, clone the grpc-go repository by running the following command:

go get google.golang.org/grpc

Then change your current directory to grpc-go/examples/route_guide:

cd $GOPATH/src/google.golang.org/grpc/examples/route_guide

You also should have the relevant tools installed to generate the server and client interface code - if you don’t already, follow the setup instructions in the Go quick start guide.

Go - for ... range

Intro for ... range clause can be used to iterate 5 types of variables: array, slice, string, map and channel, and the following sheet gives a summary of the items of for ... range loops: Type 1st Item 2nd Item Array index value Slice index value String index (rune) value (rune) Map key value Channel value Example - Iterating on slice package main import "fmt" func main() { s := [] {1,2,3} for i,v := range s { fmt. [Read More]

Mutex - Mutual Exclusion Lock

Intro Mutexes let you synchronize data access by explicit locking, without channels. Sometimes it’s more convenient to synchronize data access by explicit locking instead of using channels. The Go standard library offers a mutual exclusion lock, sync.Mutex, for this purpose. Example - Basic usage Here’s a basic example illustrating Lock and Unlock: func main() { mutex := sync.Mutex{} mutex.Lock() go func() { mutex.Lock() // Wait for main to call Unlock fmt. [Read More]

Protocol buffers

Intro Protobuf (or Protocol Buffers) is a language-agnostic and platform-neutral serialization format invented at Google. Each protocol buffer message is a small logical record of information, containing a series of name-value pairs. Unlike XML or JSON, here you first define the schema in a .proto file. They are a format like JSON but simpler, smaller, strictly typed, understandable only from the client to the server and faster to Marshall/Unmarshall. For example: [Read More]

Some common Go commands that you should know

  1. go build (see below): builds / compiles your code into a binary or library
  2. go install (see below): the same as go build, plus installs binaries into $GOPATH/bin and libraries to $GOPATH/pkg
  3. go get: downloads source code and dependencies, then perform a go install
  4. go test (see below): compile and run tests in the given package(s)
  5. go vet: check code for common coding errors

https://github.com/RHCloudServices/golang-intro

Create Go workspace

Once the Go build environment is ready, the next step is to create workspace for development: (1) Set up a new empty directory - This is where your projects can reside: mkdir $HOME/gowork (2) Use a new environment variable $GOPATH to point it: # cat /etc/profile ...... GOPATH=$HOME/gowork export GOPATH ...... If you are using bash or zsh, put the following in the respective file: export GOPATH=$HOME/gowork The workspace should contain 3 subdirectories: [Read More]