Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tools for working with Go Code

Tools for working with Go Code

Go tools are special. Go was designed from the beginning to make tools easy to write. This caused the Go ecosystem to have dozens of well built tools, such as formatters, refactoring tools, code generators, navigators, and so on.

This talk highlight some of the best and most used tools and show how to use them from an editor.

Fatih Arslan

November 09, 2015
Tweet

More Decks by Fatih Arslan

Other Decks in Programming

Transcript

  1. Development cycle • Formatting • Navigation & Insight • Refactor

    • Code generation • Linters & Checkers • Test and Benchmark • Distribution & Dependency Management
  2. goimports updates your Go import lines add missing ones remove

    unreferenced ones. play.golang.org has support for goimports too!
  3. godef How is this useful? $ godef -f main.go -o=50

    /usr/local/Cellar/go/1.5/libexec/src/fmt/print.go:263:6 $ godef -f main.go fmt.Println /usr/local/Cellar/go/1.5/libexec/src/fmt/print.go:263:6 via file offset: via expression: prints the location of the symbol referred to
  4. package main import ( "dotgo/title" "fmt" ) func main() {

    fmt.Println("Hello") fmt.Println(title.Name) } package title import "time" var Name = "DotGo! " + time.Hour.String() dotgo dotgo/title godepgraph dependency graph visualization tool
  5. Answers questions like: • What interfaces does this type satisfy?

    • Where is the definition of this identifier? • What are the possible callers of this function? • What are the exported members of this imported package? More detail: https://golang.org/s/oracle-user-manual oracle a tool for answering questions about Go source code (will be renamed to guru )
  6. oracle -pos=main.go:#124 mode <pkgs> | <*.go> $ oracle -format plain

    -pos='main.go':#151 callers ‘dotgo/' main.go:11:6: dotgo/.endLines is called from these 1 sites: main.go:7:19: static function call from dotgo/.main example usage: via file offset: oracle (cont.) callers callees implements freevars …
  7. package example func DotGo() string { return "DotGo" } func

    Colombia() string { return "Colombia" } package main import ( "example" "fmt" ) func main() { Hello(example.DotGo) Hello(example.Colombia) } func Hello(fn func() string) { fmt.Println(“Hello " + fn()) } Answers the questions: What are the possible targets of this function call? oracle - callees mode
  8. package example func DotGo() string { return "DotGo" } func

    Colombia() string { return "Colombia" } package main import ( "example" "fmt" ) func main() { Hello(example.DotGo) Hello(example.Colombia) } func Hello(fn func() string) { fmt.Println(“Hello " + fn()) } Answers the questions: What are the possible targets of this function call? oracle - callees mode
  9. package main import "fmt" func main() { msg := "Greetings\nfrom\nTurkey\n"

    var count int for i := 0; i < len(msg); i++ { if msg[i] == '\n' { count++ } } fmt.Println(count) } oracle - freevars mode Questions: What are the free variables of the selected block of code?
  10. package main import "fmt" func main() { msg := "Greetings\nfrom\nTurkey\n"

    var count int for i := 0; i < len(msg); i++ { if msg[i] == '\n' { count++ } } fmt.Println(count) } oracle - freevars mode Questions: What are the free variables of the selected block of code? Can we reuse this functionality somewhere else?
  11. package main import "fmt" func main() { msg := "Greetings\nfrom\nTurkey\n"

    var count int for i := 0; i < len(msg); i++ { if msg[i] == '\n' { count++ } } fmt.Println(count) } var msg string oracle - freevars mode Questions: What are the free variables of the selected block of code? Answer:
  12. package main import "fmt" func main() { msg := "Greetings\nfrom\nTurkey\n"

    fmt.Println(newLines(msg)) } func newLines(msg string) int { var count int for i := 0; i < len(msg); i++ { if msg[i] == '\n' { count++ } } return count } oracle - freevars mode Refactored the selection into a new function: newLines() The function argument is the free variable named: msg
  13. Navigation & Insight (more tools) • godoc: extracts and generates

    documentation for Go programs. (it's different than `go doc`). It has command-line support too, such as `go doc`, though the support for command-line usage might be dropped. • callgraph: display the the call graph of a Go program. • digraph: queries over directed graphs in text form. • godex: prints (dumps) exported information of packages or selected package objects. • ssadump: a tool for displaying and interpreting the SSA form of Go programs. • gocode: autocompletion daemon for installed Go files. • gotags: ctags compatible tag generator for Go
  14. $ gorename -from main.go::name -to identifier $ gorename -offset main.go:#123

    -to identifier via offset (better): via a query notation: gorename renames identifiers across all packages under $GOPATH
  15. gomvpkg $ gomvpkg -from github.com/fatih/foo -to github.com/fatih/bar Usage: $ gomvpkg

    -from github.com/fatih/foo -to github.com/fatih/bar -vcs_mv_cmd "git mv {{.Src}} {{.Dst}}" Supports custom move command: moves a package, updating import declarations
  16. src › tree . ├── bar │ └── example.go └──

    dotgo └── main.go package main import ( "bar" "fmt" ) func main() { fmt.Println(bar.T) } $ gomvpkg -from bar -to foo src › tree . ├── dotgo │ └── main.go └── foo └── example.go package main import ( "fmt" "foo" ) func main() { fmt.Println(foo.T) }
  17. Refactoring (other tools) • fiximports: rewrite import paths to use

    canonical package names • eg: an example-based refactoring tool • bundle: concatenates the source files of a package to include it into other packages • Oracle’s freevars mode (partially)
  18. $ jsonenums -type Status func (r Status) MarshalJSON() ([]byte, error)

    {} func (r *Status) UnmarshalJSON(data []byte) error {} status_jsonenums.go
  19. Code generation (more tools) • gojson: generate golang struct definitions

    from example JSON • gen, gotemplate, etc..: generate data structures based on templates (i.e set, slice, list, etc..) • gene: bootstrapping a Go application from scratch • sqlgen: generating sql scanners, sql statements and other helper functions • becky: Asset embedding in to your Go source code • safekeeper: replace substitute tokens with ENV variables value. • joiner: generic strings.Join implementation • optioner: a tool to generate functional options
  20. func main() { fmt.Println("%s", "Hello") fmt.Sprintf("%s", "DotGo") t := T{}

    if t.Bar != nil { } return fmt.Println("exit") } //+buildlinux package main import ( "fmt" "io" ) type T struct { Foo string `json:"foo` } func (t *T) WriteTo(r io.Writer) error { return nil } func (t *T) Bar() []byte { return []byte{} } How many errors can you detect?
  21. func main() { fmt.Println("%s", "Hello") fmt.Sprintf("%s", "DotGo") t := T{}

    if t.Bar != nil { } return fmt.Println("exit") } //+buildlinux package main import ( "fmt" "io" ) type T struct { Foo string `json:"foo` } func (t *T) WriteTo(r io.Writer) error { return nil } func (t *T) Bar() []byte { return []byte{} } Badly formed build tags Field tag’s quote is not closed WriteTo should return the # of bytes written Should be fmt.Printf Return value is not used Should be t.Bar() unreachable code How many errors can you detect?
  22. gometalinter concurrently runs go lint tools and normalize their output

    We have to many checkers and linters: • gotype • golint • errcheck • structcheck • deadcode • … How do we run all of them ?
  23. List of linters and checkers • gotype: syntactic and semantic

    analysis of Go files (similar to the front-end of Go compiler). No need to `go build` or `go install` files to show analysis errors. • golint: linter for Go source code • errcheck: check for unchecked errors (i.e: func foo() error is used as foo() instead of err := foo() ) • structcheck: find unused struct fields • unexport: unexports unused identifiers (was part of Golang Challenge #5) • gometalinter: executes checkers and linters, combines the result
  24. Testing and benchmark • go test: test packages. Enables us

    to do also benchmark, profiling, coverage … • benchcmp: benchcmp compares old and new for each benchmark. Add the result as commit message • cover: a program for analyzing the coverage profiles generated by 'go test -coverprofile=cover.out'. Moved to standard repository with Go release 1.5. • stress: is intended for catching of episodic failures. It runs a given process in parallel in a loop and collects any failures. • go-fuzz: parsing of complex inputs (github.com/dvyukov/go-fuzz)
  25. $ go test -run=NONE -bench=. ./... > old.txt # make

    changes $ go test -run=NONE -bench=. ./... > new.txt $ benchcmp old.txt new.txt benchmark old ns/op new ns/op delta BenchmarkConcat 523 68.6 -86.88% benchmark old allocs new allocs delta BenchmarkConcat 3 1 -66.67% benchcmp compares old and new for each benchmark.
  26. Distribution and Dependency Management • /vendor folder (not an external

    tool but worth mentioning) • Supported by tools like Godeps, Govendor, Glide, gvt, etc.. • godep • gb (has its own rules) • More: https://github.com/golang/go/wiki/PackageManagementTools
  27. Editor integrations We have many great editor integrations: • vim-go

    • go-mode.el (emacs) • go-plus (atom) • GoSublime (a better, Google sponsored plugin is being built.) • LiteIDE • IntelliJ Idea Plugin • Eclipse Plugin • More: https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins
  28. How to build our own tool? Lexer and Parser family:

    • go/{token, scanner, ast, parser} Type checker and abstractions • go/types • go/ssa (Static Single Assignment) Builder and formatters: • go/build • go/format • go/printer