Skip to content
Calculate cyclomatic complexities of functions in Go source code.
Go
Branch: master
Clone or download
Pull request Compare This branch is 21 commits ahead of fzipp:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd/gocyclo
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
go.mod
go.sum
package.go
package_test.go
qa_test.go

README.md

Build Status codecov

Gocyclo calculates cyclomatic complexities of functions in Go source code.

The cyclomatic complexity of a function is calculated according to the following rules:

 1 is the base complexity of a function
+1 for each 'if', 'for', 'case', '&&' or '||'

To install, run

$ go get -u github.com/gregoryv/gocyclo/...

Usage:

$ gocyclo [<flag> ...] <Go file or directory> ...

Examples:

$ gocyclo .
$ gocyclo main.go
$ gocyclo -top 10 src/
$ gocyclo -over 5 *.go
$ gocyclo -avg .

The output fields for each line are:

<complexity> <package> <function> <file:row:column>

As unit test

func TestComplexity(t *testing.T) {
	files, err := filepath.Glob("*.go")
	if err != nil {
		t.Fatal(err)
	}
	max := 5
	result, ok := gocyclo.Assert(files, max)
	if !ok {
		for _, l := range result {
			t.Log(l)
		}
		t.Errorf("Exceeded maximum complexity %v", max)
	}
}
You can’t perform that action at this time.