Go-advices

Code

  • go fmt your code, make everyone happier
  • multiple if statements can be collapsed into switch
  • use chan struct{} to pass signal
    • chan bool makes it less clear, btw struct{} is more optimal
  • prefer 30 * time.Seconds instead of time.Duration(30) * time.Seconds
  • always wrap for-select idiom to a function
  • group const declarations by type and var by logic and/or type
  • every blocking or IO function call should be cancelable or at least timeoutable
  • implement Stringer interface for integers const values
  • check your defer’s error
    1. defer func() {
    2. err := ocp.Close()
    3. if err != nil {
    4. rerr = err
    5. }
    6. }()
  • don’t use checkErr function which panics or does os.Exit
  • don’t use alias for enums ‘cause this breaks type safety

CI

Concurrency

  • best candidate to make something once in a thread-safe way is sync.Once
    • don’t user flags, mutexes, channels or atomics
  • to block forever use select{}, omit channels, waiting for a signal

Performance

  • do not omit defer
    • 200ns speedup is negligible in most cases
  • always close http body aka defer r.Body.Close()
    • unless you need leaked goroutine
  • filtering without allocating
    1. b := a[:0]
    2. for _, x := range a {
    3. if f(x) {
    4. b = append(b, x)
    5. }
    6. }
  • time.Time has pointer field time.Location and this is bad go GC
    • it’s relevant only for big number of time.Time, use timestamp instead
  • prefer regexp.MustCompile instead of regexp.Compile
    • in most cases your regex is immutable, so init it in func init
  • do not overuse fmt.Sprintf in your hot path. It is costly due to maintaining the buffer pool and dynamic dispatches for interfaces.
    • if you are doing fmt.Sprintf("%s%s", var1, var2), consider simple string concatenation.
    • if you are doing fmt.Sprintf("%x", var), consider using hex.EncodeToString or strconv.FormatInt(var, 16)
  • always discard body e.g. io.Copy(ioutil.Discard, resp.Body) if you don’t use it
    • HTTP client’s Transport will not reuse connections unless the body is read to completion and closed
      1. res, _ := client.Do(req)
      2. io.Copy(ioutil.Discard, res.Body)
      3. defer res.Body.Close()
  • don’t user defer in a loop or you’ll get a small memory leak
    • ‘cause defers will grow your stack without the reason

Build

  • strip your binaries with this command go build -ldflags="-s -w" ...
  • easy way to split test into different builds
    • use // +build integration and run them with go test -v --tags integration .

Testing

  • go test -short allows to reduce set of tests to be runned
    1. func TestSomething(t *testing.T) {
    2. if testing.Short() {
    3. t.Skip("skipping test in short mode.")
    4. }
    5. }
  • skip test deppending on architecture
    1. if runtime.GOARM == "arm" {
    2. t.Skip("this doesn't work under ARM")
    3. }
  • prefer package_test name for tests, rather than package
  • for fast benchmark comparison we’ve a benchcmp tool
  • quick replace gofmt -w -l -r "panic(err) -> log.Error(err)" .
  • go list allows to find all direct and transitive dependencies
    • go list -f '{{ .Imports }}' package
    • go list -f '{{ .Deps }}' package

Misc

  • dump goroutines https://stackoverflow.com/a/27398062/433041
    1. go func() {
    2. sigs := make(chan os.Signal, 1)
    3. signal.Notify(sigs, syscall.SIGQUIT)
    4. buf := make([]byte, 1<<20)
    5. for {
    6. <-sigs
    7. stacklen := runtime.Stack(buf, true)
    8. log.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
    9. }
    10. }()
  • check interface implementation during compilation
    1. var _ io.Reader = (*MyFastReader)(nil)
  • if a param of len is nil then it’s zero
  • anonymous structs are cool
    1. var hits struct {
    2. sync.Mutex
    3. n int
    4. }
    5. hits.Lock()
    6. hits.n++
    7. hits.Unlock()
  • httputil.DumpRequest is very useful thing, don’t create your own
ft_authoradmin  ft_create_time2017-11-19 15:39
 ft_update_time2017-11-19 15:39