Improve your go code like a cow.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Catalogue [−]

    1. Gofmt
    2. Gocyclo
    3. Interfacer
    4. Deadcode
    5. Gotype
    6. Misspell
    7. Staticcheck
    8. Gosimple
    9. Goconst

Original: Lint your #golang code like a mad man!, author: Arsham Shirvani

I use the following tool to improve my code in addition to vendor folders. My operating system is GNU/Linux , but a little modification of the script should also be able to run on your operating system. I use glide to handle dependencies (vendor), but you can also use your package dependency management tool to replace glide nv , this command lists all the folders except Vender (Translator press: Go 1.9 can be used directly ./... , it will exclude vendor folder). In some cases glide nv it doesn't fit, so I used it in vintage style.

Note I use $ the prompt as a shell.

Gofmt

The Go installer comes with gofmt tools that you can use to format code and maintain a consistent code style:

1
"*.go" "./vendor/*" ". git/*" - S - D

Gocyclo

Gocyclo is used to check the complexity of a function.

Installation:

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

Use:

1
 A - d * | Grep-v vendor)

The above command lists all functions with a complexity greater than 12. You can also present the most complex of several:

1
Ten - d * | Grep-v vendor)

Interfacer

Interfacer is an interesting tool that, according to the author, says:

This tool provides recommendations for the type of interface, in other words, it warns against code that does not need to be defined as a specific type

Installation:

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

Use:

1
$ Interfacer $ (Glide nv)

Translator by: See the official example to understand the role of this tool:

1234567
func ProcessInput (f *os. File) Error {        B, err: = Ioutil. ReadAll (f)        ifnil {                return err        }        return processbytes (b)}
12
$ Interfacer $ (go list ... | grep-v/vendor/) FOO.GO:: F can beio. Reader

Deadcode

Deadcode will tell you which snippets are useless at all.

Installation:

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

Use:

1
find . -typed"./vendor/*" | Xargs Deadcode

Gotype

Gotype will perform semantic (semantic) and syntactic (syntactic) parsing of Go files and packages, which is a tool provided by Google.

Installation:

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

Use:

1
"*.go" "./vendor/*" ". git/*" -print0 | Xargs-0 -A

Misspell

Misspell is used for spell checking, which is very helpful to the students who are not very proficient in English.

Installation:

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

Use:

1
"./vendor/*" -print0 | Xargs-0 misspell

Staticcheck

Staticcheck is a super-bull tool that provides a huge number of static checks, just like the ReSharper of the C # ecosystem.

Installation:

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

Use:

1
$ Staticcheck $ (Glide nv)

Gosimple

Gosimple provides information to help you understand which code can be simplified.

Installation:

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

Use:

1
$ Gosimple $ (Glide nv)

Translator by: In fact, this tool and the above Staticcheck tools have been merged into the same project: Go-tools, this project provides a very good tool, also includes,,, and structlayout-optimize unused rdeps keyify so on, the value of you to explore.

Goconst

Goconst will look for duplicate strings, which can be extracted as constants.

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

Use:

1
$ goconst./... | Grep-v Vendor

These are some of the tools listed by the author, and I have a lot of overlap with the tools listed in one of my previous articles: using tools to check your code, actually I have used a lot of the code in the project, very helpful, I hope you can gain after reading, Add these tools to your makefile file quickly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.