Giter VIP home page Giter VIP logo

example-linter's Introduction

This is an example linter that can be compiled into a plugin for golangci-lint.

To use this:

Create the Plugin From This Linter

  1. Download the source code *
  2. From the root project directory, run go build -buildmode=plugin plugin/example.go.
  3. Copy the generated example.so file into your project or to some other known location of your choosing. **

Create a Copy of golangci-lint that Can Run with Plugins

In order to use plugins, you'll need a golangci-lint executable that can run them. Plugin dependencies defined in the go.mod file MUST have a matching version (or hash) as the same dependency in th golangci-lint binary if the dependency is used in both. Because of the high probability of this both using the same dependency, it is recommended to use a locally built binary. To do so:

  1. Download golangci-lint source code
  2. From the projects root directory, run make
  3. Copy the golangci-lint executable that was created to your path, project, or other location

Configure Your Project for Linting

If you already have a linter plugin available, you can follow these steps to define its usage in a projects .golangci.yml file. If you're looking for instructions on how to configure your own custom linter, they can be found further down.

  1. If the project you want to lint does not have one already, copy the .golangci.yml to the root directory.
  2. Adjust the yaml to appropriate linters-settings:custom entries as so:
linters-settings:
 custom:
  example:
   path: /example.so
   description: The description of the linter
   original-url: github.com/golangci/example-linter

That is all the configuration that is required to run a custom linter in your project. Custom linters are enabled by default, but abide by the same rules as other linters. If the disable all option is specified either on command line or in .golang.yml files linters:disable-all: true, custom linters will be disabled; they can be re-enabled by adding them to the linters:enable list, or providing the enabled option on the command line, golangci-lint run -Eexample.

To Create Your Own Custom Linter

Your linter must implement one or more golang.org/x/tools/go/analysis.Analyzer structs. Your project should also use go.mod. All versions of libraries that overlap golangci-lint (including replaced libraries) MUST be set to the same version as golangci-lint. You can see the versions by running go version -m golangci-lint.

You'll also need to create a go file like plugin/example.go. This MUST be in the package main, and define a variable of name AnalyzerPlugin. The AnalyzerPlugin instance MUST implement the following interface:

type AnalyzerPlugin interface {
    GetAnalyzers() []*analysis.Analyzer
}

The type of AnalyzerPlugin is not important, but is by convention type analyzerPlugin struct {}. See plugin/example.go for more info.

To build the plugin, from the root project directory, run go build -buildmode=plugin plugin/example.go. This will create a plugin *.so file that can be copied into your project or another well known location for usage in golangci-lint.

* Sorry, I haven't found a way to enable go get functionality for plugins yet. If you know how, let me know!

** Alternately, you can use the -o /path/to/location/example.so output flag to have it put it there for you.

example-linter's People

Contributors

dbraley avatar jpalawaga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.