Go

Good Read:

SETUP

brew install go
mkdir $HOME/go && cd $HOME/go && mkdir bin pkg src && cd -
echo "export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
" >> ~/.zshrc
source ~/.zshrc  

Interfaces

Reference:

Implements of io.Reader

strings.NewReader("some string\n")
bytes.NewReader([]byte{})

Implements of io.Writer

buff := bytes.Buffer{}
buff.WriteString("some string")

Reader to ReaderClose

ioutil.NopCloser(reader)

PROFILING

  • runtime/pprof
  • github.com/pkg/profile: simple profiling library (for research/example)

Vendoring

Using glide

brew install glide

glide create # create glide.yaml
glide install # start pulling
glide up # update version

Using dep

brew install dep

dep init
dep ensure
dep ensure -add github.com/foo/bar github.com/baz/quux
dep ensure -update

GOOS/GOARCH

More detail list

Or run command

go tool dist list
$GOOS $GOARCH
darwin amd64
linux amd64
windows amd64

Profiling

Context

  • WithValue: the process may need this value
  • WithCancel: the process can be cancelled
  • WithDeadline: the process can be expired
  • WithTimeout: same with WithDeadline

Module

Debug

// ElapsedTimeFn to print elapsed time of function
func ElapsedTimeFn(name string) func() {
	start := time.Now()
	return func() {
		log.Debugf("%s took %v\n", name, time.Since(start))
	}
}