59
Tools bringing happiness

Tools Bringing Happiness

Embed Size (px)

Citation preview

Toolsbringing happiness

Code Inspection Tools

• gotype• errcheck• go vet• golint• defercheck, structcheck, varcheck

gotypehttps://godoc.org/golang.org/x/tools/cmd/gotype

“syntactic and semantic analysis of Go files and packages like the front-end of a Go compiler”

go get golang.org/x/tools/cmd/gotype

errcheckhttps://github.com/kisielk/errcheck

“Errcheck is a program for checking for unchecked errors in go programs”

go get github.com/kisielk/errcheck

errcheckhttps://github.com/kisielk/errcheck

go vethttp://godoc.org/golang.org/x/tools/cmd/vet

“reports suspicious constructs”

go get golang.org/x/tools/cmd/vet

• Suspicious calls to functions in the Printf family• Locks that are erroneously passed by value• Unreachable code• Shadowed variables...

Comparisons between functions and nil

go vet

comparison of function useless != nil is always true

Comparisons between functions and nil

go vet

unreachable code

Unreachable code

go vet

golinthttps://github.com/golang/lint

“Golint makes suggestions for many of the mechanically checkable items listed in Effective Go and the CodeReviewComments wiki page.”

go get github.com/golang/lint/golint

golint

should replace x += 1 with x++

x++ and x--

golint

• error var SomeErr should have name of the form ErrFoo• exported var SomeErr should have comment or be unexported

global error vars & doc comments

defercheck, structcheck, varcheckhttps://github.com/opennota/check

• defercheckFind repeating defers

• structcheckFind unused struct fields

• varcheckFind unused global variables and constants

go get github.com/opennota/check/cmd/...

SublimeLinterhttps://github.com/SublimeLinter/SublimeLinter3

SublimeLinter-contrib-gotypehttps://packagecontrol.io/packages/SublimeLinter-contrib-gotype

SublimeLinter-contrib-govethttps://packagecontrol.io/packages/SublimeLinter-contrib-govet

SublimeLinter-contrib-golinthttps://packagecontrol.io/packages/SublimeLinter-contrib-golint

SublimeLinter-contrib-errcheckhttps://github.com/alecthomas/SublimeLinter-contrib-errcheck

SublimeLinterhttps://github.com/SublimeLinter/SublimeLinter3

SublimeLinterhttps://github.com/SublimeLinter/SublimeLinter3

Toggle Linter... ^⌘T

SublimeLinterhttps://github.com/SublimeLinter/SublimeLinter3

Code Comprehension Tools

• godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

• oraclehttp://golang.org/s/oracle-user-manualhttps://github.com/fzipp/pythia

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

godoc -analysishttps://golang.org/lib/godoc/analysis/help.html

$ godoc -http=:6060 -analysis=type,pointer

oraclehttp://golang.org/s/oracle-user-manual

go get golang.org/x/tools/cmd/oracle

$ oracle \

-pos='./src/github.com/boltdb/bolt/tx.go:#1630' \

-format=plain \

callers github.com/boltdb/bolt

./src/github.com/boltdb/bolt/tx.go:66:15: (*github.com/boltdb/bolt.Tx).Size is called from these 1 sites:

./src/github.com/boltdb/bolt/tx.go:285:35: static method call from (*github.com/boltdb/bolt.Tx).WriteTo

oracle + pythiahttps://github.com/fzipp/pythia

$ pythia -v github.com/boltdb/bolt/cmd/bolt github.com/boltdb/bolt

go get github.com/fzipp/pythia

Refactoring Tools

• gorename• gofmt -r• ed

gorenamehttps://godoc.org/golang.org/x/tools/cmd/gorename

“The gorename command performs precise type-safe renaming of identifiers in Go source code.”

go get golang.org/x/tools/cmd/oracle

$ gorename -v -from='"demo".handler' -to='rootHandler'❚

$ gorename -v -from='"demo".handler' -to='rootHandler'

Renamed 2 occurrences in 1 file in 1 package.

$ ❚

gofmt -rhttps://golang.org/cmd/gofmt/

“Apply the rewrite rule to the source before reformatting”

$ gofmt -w -r 'x.Write([]byte(y)) -> x.Write([]byte(strings.ToLower(y)))' src/demo/main.go ❚

$ gofmt -w -r 'x.Write([]byte(y)) -> x.Write([]byte(strings.ToLower(y)))' src/demo/main.go

$ ❚

$ gofmt -w -r 'http.ListenAndServe(x, y) ->

http.ListenAndServe(x, y, "BAD THING")' src/demo/main.go ❚

$ gofmt -w -r 'http.ListenAndServe(x, y) ->

http.ListenAndServe(x, y, "BAD THING")' src/demo/main.go

$ ❚

eg

“Example-based refactoring of expressions...”

go get golang.org/x/tools/cmd/eg

1

2

3

5

$ eg -w -t eg-template.go demo ❚

6

$ eg -w -t eg-template.go demo

=== eg/src/demo/demo.go (1 matches)

$ ❚

7

$ eg -w -t eg-template.go demo

=== eg/src/demo/demo.go (1 matches)

$ gorename -from='"demo".NewPoint' -to='NewPoint__OLD__' ❚

8

$ eg -w -t eg-template.go demo

=== eg/src/demo/demo.go (1 matches)

$ gorename -from='"demo".NewPoint' -to='NewPoint__OLD__'

$ ❚

9

$ eg -w -t eg-template.go demo

=== eg/src/demo/demo.go (1 matches)

$ gorename -from='"demo".NewPoint' -to='NewPoint__OLD__'

$ gorename -from='"demo".NewPoint__NEW__' -to='NewPoint' ❚

10

$ eg -w -t eg-template.go demo

=== eg/src/demo/demo.go (1 matches)

$ gorename -from='"demo".NewPoint' -to='NewPoint__OLD__'

$ gorename -from='"demo".NewPoint__NEW__' -to='NewPoint'

$ ❚

gocyclohttps://github.com/fzipp/gocyclo

“calculates cyclomatic complexities of functions”

1 is the base complexity of a function

+1 for each 'if', 'for', 'case', '&&' or '||'

MOAR TOOLZ

• https://godoc.org/golang.org/x/tools• https://github.com/golang/tools/tree/master/cmd• http://dominik.honnef.co/posts/2014/12/an_incomplete_list_of_go_tools/

package main

import "fmt"

func main() {

contact := struct {

name, email string

}{

"Konstantin Cherkasov",

"[email protected]",

}

fmt.Println(contact)

}