Golang

Learn Golang

  1. Learn Golang from the official tutorial site: Tour of Go.

Code Style

  1. Code interpretability trumps everything else.
  2. Please conform to the Golang code style established in the following articles.

Naming

  • The convention in Go is to use mixedCase rather than underscores to write multiword names.
  • Packages are given lower case, single-word names; there should be no need for underscores or mixedCase.
  • Naming guide:

    Type Internal Exported
    Packages   lowercase
    Variables mixedCase MixedCase
    Function/Method Names mixedCase MixedCase
    Function/Method Parameters mixedCase  
    Structs mixedCase MixedCase
    Exceptions   MixedCase
    Constants mixedCase MixedCase

Code Documentation - GoDoc

  1. GoDoc extracts and generates documentation for Go programs.
  2. It runs as a web server and presents the documentation as a web page.
  3. Usage:
     $ godoc [flag]
    
     [flag]
     -goroot=$GOROOT  : Go root directory
     -http=addr       : HTTP service address (e.g., '127.0.0.1:6060' or just ':6060')
    
  4. Example:
     $ godoc -http=:6060
    
  5. By default, godoc looks at the packages it finds via $GOROOT and $GOPATH (if set). This behavior can be altered by providing an alternative $GOROOT with the -goroot flag.

Profiling

  1. To profile a Go code using a web interface, run:
     $ go tool pprof -http=localhost:8080 /tmp/cpu.pprof
     $ go tool pprof -http=localhost:8080 /tmp/mem.pprof
    

    Here, /tmp/cpu.pprof and /tmp/mem.pprof are the cpu and memory profiler output files.

  2. To see time spent on each line, run:
     $ go tool pprof /tmp/cpu.pprof
     $ list <packageName>
    

Updated:

Leave a comment