checkstyle for go

Overview

go-checkstyle

Build Status

checkstyle is a style check tool like java checkstyle. This tool inspired by java checkstyle, golint. The style refered to some points in Go Code Review Comments.

Install

go get github.com/qiniu/checkstyle/gocheckstyle

Run

gocheckstyle -config=.go_style dir1 dir2

Config

config is json file like the following:

{
    "file_line": 500,
    "func_line": 50,
    "params_num":4,
    "results_num":3,
    "formated": true,
    "pkg_name": true,
    "camel_name":true,
    "ignore":[
        "a/*",
        "b/*/c/*.go"
    ],
    "fatal":[
        "formated"
    ]
}

Add to makefile

check_go_style:
	bash -c "mkdir -p checkstyle; cd checkstyle && export GOPATH=`pwd` && go get github.com/qiniu/checkstyle/gocheckstyle"
	checkstyle/bin/gocheckstyle -config=.go_style dir1 dir2

Integrate with jenkins checkstyle plugin

excute in shell

    mkdir -p checkstyle; cd checkstyle && export GOPATH=`pwd` && go get github.com/qiniu/checkstyle/gocheckstyle"
    checkstyle/bin/gocheckstyle -reporter=xml -config=.go_style dir1 dir2 2>gostyle.xml

then add postbuild checkstyle file gostyle.xml

Run checkstyle with one or more filenames or directories. The output of this tool is a list of suggestions. If you need to force obey the rule, place it in fatal.

Checkstyle's difference with other tools

Checkstyle differs from gofmt. Gofmt reformats Go source code, whereas checkstyle prints out coding style suggestion.

Checkstyle differs from golint. Checkstyle check file line/function line/param number, could be configed by user.

You might also like...
Comments
  • Enable ** glob patterns for ignored files & directories

    Enable ** glob patterns for ignored files & directories

    This is a great tool, but it keeps on alerting on generated code that I don't have control over, and therefore I cannot use it to break the build without manual review. This PR uses a drop-in replacement for filepath.Match that supports the doublestar file glob so that we can ignore deeply nested & generated files & directories.

    opened by tomcz 1
  • Report errors when functions miss body

    Report errors when functions miss body

    Environment

    go 1.6.2

    Problem

    When a function declaration miss {} block like "func Foo()", checkstyle will panic. It caused my checkcode script abort unexpectedly.

    panic: runtime error: invalid memory address or nil pointer dereference

    Reason

    As the comments described in go standard library ast, the first param of func ast.Inspect must not be nil

    
    // Inspect traverses an AST in depth-first order: It starts by calling
    // f(node); node must not be nil. If f returns true, Inspect invokes f
    // recursively for each of the non-nil children of node, followed by a
    // call of f(nil).
    //
    func Inspect(node Node, f func(Node) bool) {
        Walk(inspector(f), node)
    }
    

    but when a function miss its body, decl.Body is nil. panic at checkstyle.go:354

    func (f *file) checkFileContent() {
            .....
            switch decl := v.(type) {
            case *ast.FuncDecl:
                f.checkFunctionDeclare(decl)
                ast.Inspect(decl.Body, func(node ast.Node) bool { //panic
                    switch decl2 := node.(type) {
                    case *ast.GenDecl:
                        f.checkGenDecl(decl2, false)
                    case *ast.FuncDecl:
                        f.checkFunctionDeclare(decl2)
                    case *ast.AssignStmt:
                        f.checkAssign(decl2)
                    case *ast.StructType:
                        f.checkStruct(decl2)
                    }
                    return true
                })
                .........
    }
    

    Solution

    Add new method "checkFunctionBody" to Check function body, and generate a relative problem when body is nil.

    Now

    2016/07/29 00:10:12 ========= There are 1 fatal problems ========= 2016/07/29 00:10:12 Foo.go:10:1: func Foo expected block '{}'

    opened by buptmiao 1
  • Add some example cases to the readme

    Add some example cases to the readme

    Without downloading the package and testing it myself, it's not clear to me how this tool differs from golint and gofmt -s. Could you add some examples to the readme?

    opened by hermanschaaf 0
Owner
Qiniu Cloud
Connect Data, Redefine Value.
Qiniu Cloud