Writing Custom Linter in Go

Writing Custom Linter in Go

Write a custom linter in Go. Writing linters is simple. I was surprised how it’s easy to write a Go linter. Today, we’ll write a linter that will calculate the cyclomatic complexity of the Golang code. We have a fully functional Go linter! Your linter to golangci-lint. Here’s an example of the linter we’ve built.

Writing linters is simple. I was surprised how it’s easy to write a Go linter. Today, we’ll write a linter that will calculate the cyclomatic complexity of the Go code.

What is cyclomatic complexity?

Cyclomatic complexity is a software metric used to indicate the complexity of a program. ref

The idea is simple — every time we find any control flow statements we increase the complexity by one. I know I oversimplified it a bit but I don’t want to overwhelm you with unnecessary details.

There are a few steps we should follow to write our custom linter. Firstly, we can create a test that will check if our linter works or not. Let’s put him into pkg/analyzer/analyzer_test.go file.

package analyzer

import (
    "os"
    "path/filepath"
    "testing"

    "golang.org/x/tools/go/analysis/analysistest"
)

func TestAll(t *testing.T) {
    wd, err := os.Getwd()
    if err != nil {
        t.Fatalf("Failed to get wd: %s", err)
    }

    testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata")
    analysistest.Run(t, testdata, NewAnalyzer(), "complexity")
}

The analysistest.Run() function is a helper that simplifies testing linters. What it does is running our linter on package complexity in testate folder. We use NewAnalyzer() function that will return instance of our analyzer. Let's add it to pkg/analyzer/analyzer.go.

go golang linter

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

Go Tutorial (Golang) - Looping Constructs in Golang | Golang For Loop

In this video we will see the Looping Constructs in Golang. Golang For Loop. Go is an open source programming language which was originally developed by Google. In this Go Tutorial we will Learn Go from the Basics with Code Examples. Go is a statically-typed language.

Golang Array Example | Arrays in Go Tutorial Explained

Golang array is a fixed-size collection of items of the same type. The items of an array are stored sequentially and can be accessed using their index.

Go Tutorial (Golang) 18 - tags in Golang | How To Use Struct Tags in Go

Go struct tags are annotations that are shown after the type in a Go struct declaration. Golang tag for a field allows us to attach meta-information to the field which we can acquire using reflection

Go Tutorial (Golang) - Looping Constructs in Golang | For Range loop in Golang

In this video we will see the Looping Constructs in Golang - Golang For for-range Loop. o is an open source programming language which was originally developed by Google. In this Go Tutorial we will Learn Go from the Basics with Code Examples. Go is a statically-typed language. Go has a syntax similar to C. Go has built-in concurrency.