Go
CLI commands
# help
go
go help
# version
go version
# run file
go run hello.go
# build file
go build hello.go
# build with profiling
go build -gcflags=-m hello.go
# cli doc
go doc fmt Printf
# run tests
go test
go test -v
# run test by strict name
go test -run TestSumAndMult
# run tests by RegExp
go test -run TestSum -v
# go generate [util-name] [options]
# mark file
# //go:generate stringer -type=Pill
# execute generate
go generate path/to/original.go
# run generated
go run path/to/original.go path/to/generated.go
ENV
- GOPATH - project path
- GOROOT - Go path
Project structure
bin/
- compiled files
pkg/
- service files
src/
- project code
Management dependencies
# enable go mod
export GO111MODULE=on
# init go mod
go mod init
Debug
Delve is a debugger for the Go programming language.
Profiling
// online diagnosis
import _ "net/http/pprof"
# profile code
go tool pprof -seconds 5 http://server/debuf/pprof/profile
# profile benchmark tests
go test -bench=.
# run benchmark with memory info
go test -bench=. -benchmem
# run benchmark with memory info and file output
go test -bench=YourBenchmarkNameHere -benchmem -memprofile=mem.out
# run benchmark with cpu info output
go test -bench=YourBenchmarkNameHere -cpuprofie cpu.out
go test -bench=. -cpuprofie cpu.out
go tool pprof -alloc_space your_compiled_binary_file mem.out
go tool pprof your_compiled_binary_file cpu.out
list
list yourFuncName
Testing
Web Frameworks
ORM
WebSocket
JSON
# install package
go get -u github.com/mailru/easyjson/...
# run generate Marshal/Unmarshal methods for structs
easyjson -all ./path/to/file.go
gRPC
gRPC