[譯]像牛人一樣改進你的Go代碼

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

目錄 [−]

  1. gofmt
  2. gocyclo
  3. interfacer
  4. deadcode
  5. gotype
  6. misspell
  7. staticcheck
  8. gosimple
  9. goconst

原文: Lint your #golang code like a mad man!, 作者: Arsham Shirvani

我使用下面的工具來改進My Code,除了vendor檔案夾。我的作業系統是GNU/Linux,但是稍微修改一下指令碼應該也能運行在你的作業系統上。我使用glide來處理依賴(vendor),但你也可以使用你的包依賴管理工具來替換glide nv, 這個命令列出了所有的檔案夾,除了vender (譯者按: Go 1.9中可以直接使用./...,它會排除vendor檔案夾)。有些情況下glide nv不適合,所以我使用了它的老式風格。

注意我使用$作為shell的提示符。

gofmt

Go安裝程式中內建了gofmt工具,可以使用它來格式化代碼,保持一致的代碼風格:

1
$ find . -name "*.go" -not -path "./vendor/*" -not -path ".git/*" | xargs gofmt -s -d

gocyclo

gocyclo 用來檢查函數的複雜度。

安裝:

1
$ go get -u github.com/fzipp/gocyclo

使用:

1
$ gocyclo -over 12 $(ls -d */ | grep -v vendor)

上面的命令列出了所有複雜度大於12的函數。你還可以提出最複雜的幾個:

1
$ gocyclo -top 10 $(ls -d */ | grep -v vendor)

interfacer

interfacer 是一個有趣的工具,依照作者所說:

這個工具提供介面類型的建議,換句話說,它會對可以本沒有必要定義成具體的類型的代碼提出警告

安裝:

1
$ go get -u github.com/mvdan/interfacer/cmd/interfacer

使用:

1
$ interfacer $(glide nv)

譯者按:看官方的例子就明白這個工具的作用了:

1234567
func ProcessInput(f *os.File) error {        b, err := ioutil.ReadAll(f)        if err != nil {                return err        }        return processBytes(b)}
12
$ interfacer $(go list ./... | grep -v /vendor/)foo.go:10:19: f can be io.Reader

deadcode

deadcode會告訴你哪些程式碼片段根本沒用。

安裝:

1
$ go get -u github.com/tsenart/deadcode

使用:

1
$ find . -type d -not -path "./vendor/*" | xargs deadcode

gotype

gotype會對go檔案和包進行語義(semantic)和句法(syntactic)的分析,這是google提供的一個工具。

安裝:

1
$ go get -u golang.org/x/tools/cmd/gotype

使用:

1
$ find . -name "*.go" -not -path "./vendor/*" -not -path ".git/*" -print0 | xargs -0 gotype -a

misspell

misspell用來拼字檢查,對國內英語不太熟練的同學很有協助。

安裝:

1
$ go get -u github.com/client9/misspell

使用:

1
$ find . -type f -not -path "./vendor/*" -print0 | xargs -0 misspell

staticcheck

staticcheck是一個超牛的工具,提供了巨多的靜態檢查,就像 C#生態圈的 ReSharper 一樣。

安裝:

1
$ go get -u honnef.co/go/staticcheck/cmd/staticcheck

使用:

1
$ staticcheck $(glide nv)

gosimple

gosimple 提供資訊,協助你瞭解哪些代碼可以簡化。

安裝:

1
$ go get -u honnef.co/go/simple/cmd/gosimple

使用:

1
$ gosimple $(glide nv)

譯者按: 事實上這個工具和上面的staticcheck工具已經合并為同一個項目了:go-tools,這個項目提供了非常好的工具, 還包括 structlayout-optimizeunusedrdepskeyify等,值的你去探索。

goconst

goconst 會尋找重複的字串,這些字串可以抽取成常量。

1
$ go get -u github.com/jgautheron/goconst/cmd/goconst

使用:

1
$ goconst ./… | grep -v vendor

以上是作者列出的一些工具, 和我以前的一篇文章中列出的工具有很多重合的: 使用工具檢查你的代碼, 事實上我在項目中已經使用了文中很多的代碼,非常非常的有協助,希望你在閱讀後能有所收穫,快將這些工具加入到你的Makefile檔案中吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.