Golang

Installing on Linux

sudo add-apt-repository ppa:gophers/archive -y
sudo apt-get update
sudo apt-get install golang-1.10-go -y

Installing golang/dep (Package Manager)

# OSX
brew install dep

# Linux
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

Install all dependencies recursively

To install all dependencies of a golang project or golang projects recursively with the go get command, change directory into the project and simply run:

go get ./...

Cross-platform compilation

# For example you can do this from OSX machine
GOOS=linux GOARCH=amd64 go build

possible GOOS value:

  • darwin
  • linux
  • windows

possible GOARCH value:

  • 386
  • amd64

Manipulating Slice

  1. https://github.com/golang/go/wiki/SliceTricks
  2. https://stackoverflow.com/questions/25025409/delete-element-in-a-slice

Manipulating JSON

https://blog.golang.org/json-and-go

Rewriting Request Body

We need to modify the content length if we modify http request body.

req.ContentLength = int64(len(b))
req.Body = ioutil.NopCloser(bytes.NewReader(b))

See this.

Mocking

Do this inside one of your project

dep ensure -add github.com/golang/mock/gomock
go get github.com/golang/mock/mockgen
# Ensure your $GOPATH/bin is in your $PATH
mkdir mock
mockgen -destination=mock/mock_<file_name>.go -package=mock <package> <interface>

Debugging Memory Leak

References:

Note: be mindful of golang’s allocation. Use stack allocation whenever possible.

Limit of HTTP Handler Concurrent Connection

https://stackoverflow.com/questions/47385692/limited-concurrent-connections-in-go

Useful Standard Library

https://golang.org/pkg/time/#Sleep

Useful Libraries

  1. CLI App Framework

Best Practices