Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02a8a2a

Browse files
committed
chore: clean
1 parent5b57aa0 commit02a8a2a

File tree

8 files changed

+99
-96
lines changed

8 files changed

+99
-96
lines changed

‎.gitignore‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.idea/
2+
/.vscode/
3+
/dist/
4+
.DS_Store
5+
/*.pprof
6+
/*.txt
7+
/golangci-lint
8+
/golangci-lint.exe
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
linters-settings:
22
custom:
33
example:
4-
# Path is required
4+
# Path is required
55
path:example.so
6-
#description is optional
6+
# Description is optional
77
description:The description of the linter. This is optional, but shows up when running `golangci-lint linters`.
8-
#original-url is optional, and is only used for documentation purposes.
8+
# Original-url is optional, and is only used for documentation purposes.
99
original-url:github.com/golangci/example-linter
10+
1011
linters:
1112
disable-all:true
1213
enable:
1314
-example
14-
# Typecheck is how golangci-lint reports compile errors, so should always be enabled.
15-
-typecheck
15+
# Typecheck is how golangci-lint reports compile errors, so should always be enabled.
16+
-typecheck

‎README.md‎

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
11
This is an example linter that can be compiled into a plugin for`golangci-lint`.
22

3-
To use this:
4-
53
###Create the Plugin From This Linter
64

7-
1. Download the source code\*
5+
1. Download the source code
86
2. From the root project directory, run`go build -buildmode=plugin plugin/example.go`.
9-
3. Copy the generated`example.so` file into your project or to some other known location of your choosing.\**
10-
7+
3. Copy the generated`example.so` file into your project or to some other known location of your choosing.[^1]
118

129
###Create a Copy of`golangci-lint` that Can Run with Plugins
1310

14-
In order to use plugins, you'll need a golangci-lint executable that can run them. Plugin dependencies defined in the
15-
`go.mod` file MUST have a matching version (or hash) as the same dependency in th`golangci-lint` binary if the
16-
dependency is used in both. Because of the high probability of this both using the same dependency, it is recommended
17-
to use a locally built binary. To do so:
11+
In order to use plugins, you'll need a golangci-lint executable that can run them.
12+
13+
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.
14+
15+
Because of the high probability of this both using the same dependency, it is recommended to use a locally built binary.
16+
17+
To do so:
1818

1919
1. Download[golangci-lint](https://github.com/golangci/golangci-lint) source code
20-
2. From the projects root directory, run`make`
20+
2. From the projects root directory, run`make build`
2121
3. Copy the`golangci-lint` executable that was created to your path, project, or other location
2222

2323
###Configure Your Project for Linting
2424

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

29-
1. If the project you want to lint does not have one already, copy the[.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml)
30-
to the root directory.
31-
2. Adjust the yaml to appropriate`linters-settings:custom` entries as so:
32-
```
33-
linters-settings:
34-
custom:
35-
example:
36-
path: /example.so
37-
description: The description of the linter
38-
original-url: github.com/golangci/example-linter
39-
```
27+
If you're looking for instructions on how to configure your own custom linter, they can be found further down.
28+
29+
1. If the project you want to lint does not have one already, copy the[.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) to the root directory.
30+
2. Adjust the YAML to appropriate`linters-settings.custom` entries as so:
31+
```yml
32+
linters-settings:
33+
custom:
34+
example:
35+
path:/example.so
36+
description:The description of the linter
37+
original-url:github.com/golangci/example-linter
38+
```
4039
41-
That is all the configuration that is required to run a custom linter in your project. Custom linters are enabled by default,
42-
but abide by the same rules as other linters. If the disable all option is specified either on command line or in
43-
`.golang.yml` files`linters:disable-all: true`, custom linters will be disabled; they can be re-enabled by adding them
44-
to the`linters:enable` list, or providing the enabled option on the command line,`golangci-lint run -Eexample`.
40+
That is all the configuration that is required to run a custom linter in your project.
41+
42+
Custom linters are enabled by default, but abide by the same rules as other linters.
43+
44+
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;
45+
they can be re-enabled by adding them to the `linters:enable` list,
46+
or providing the enabled option on the command line, `golangci-lint run -Eexample`.
4547

4648
### To Create Your Own Custom Linter
4749

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

52-
You'll also need to create a go file like`plugin/example.go`. This MUST be in the package`main`, and define a
53-
variable of name`AnalyzerPlugin`. The`AnalyzerPlugin` instance MUST implement the following interface:
54-
```
52+
Your project should also use `go.mod`.
53+
54+
All versions of libraries that overlap `golangci-lint` (including replaced libraries) MUST be set to the same version as `golangci-lint`.
55+
You can see the versions by running `go version -m golangci-lint`.
56+
57+
You'll also need to create a go file like `plugin/example.go`.
58+
59+
This MUST be in the package `main`, and define a variable of name `AnalyzerPlugin`.
60+
The `AnalyzerPlugin` instance MUST implement the following interface:
61+
62+
```go
5563
type AnalyzerPlugin interface {
5664
GetAnalyzers() []*analysis.Analyzer
5765
}
5866
```
59-
The type of`AnalyzerPlugin` is not important, but is by convention`type analyzerPlugin struct {}`. See
60-
[plugin/example.go](https://github.com/golangci/example-plugin-linter/plugin/example.go) for more info.
6167

62-
To build the plugin, from the root project directory, run`go build -buildmode=plugin plugin/example.go`. This will create a plugin`*.so`
63-
file that can be copied into your project or another well known location for usage in golangci-lint.
68+
The type of `AnalyzerPlugin` is not important, but is by convention `type analyzerPlugin struct {}`.
69+
See [plugin/example.go](https://github.com/golangci/example-plugin-linter/plugin/example.go) for more info.
70+
71+
To build the plugin, from the root project directory, run `go build -buildmode=plugin plugin/example.go`.
6472

65-
\* Sorry, I haven't found a way to enable`go get` functionality for plugins yet. If you know how, let me know!
73+
This will create a plugin `*.so` file that can be copied into your project or another well known location for usage in golangci-lint.
6674

67-
\** Alternately, you can use the`-o /path/to/location/example.so` output flag to have it put it there for you.
75+
[^1]:Alternately, you can use the `-o /path/to/location/example.so` output flag to have it put it there for you.

‎example.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package linters
22

33
import (
44
"go/ast"
5-
"golang.org/x/tools/go/analysis"
65
"strings"
6+
7+
"golang.org/x/tools/go/analysis"
78
)
89

910
varTodoAnalyzer=&analysis.Analyzer{
@@ -16,8 +17,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
1617
for_,file:=rangepass.Files {
1718
ast.Inspect(file,func(n ast.Node)bool {
1819
ifcomment,ok:=n.(*ast.Comment);ok {
19-
ifstrings.HasPrefix(comment.Text,"// TODO:")||
20-
strings.HasPrefix(comment.Text,"// TODO():") {
20+
ifstrings.HasPrefix(comment.Text,"// TODO:")||strings.HasPrefix(comment.Text,"// TODO():") {
2121
pass.Report(analysis.Diagnostic{
2222
Pos:comment.Pos(),
2323
End:0,
@@ -27,8 +27,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
2727
})
2828
}
2929
}
30+
3031
returntrue
3132
})
3233
}
34+
3335
returnnil,nil
3436
}

‎go.mod‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
modulegithub.com/dbraley/example-linter
1+
modulegithub.com/golangci/example-linter
22

33
// All versions here need to be the same as in golangci-lint/mod.go if present
44

5-
go1.16
5+
go1.19
66

77
require (
8-
github.com/stretchr/testifyv1.7.0
9-
golang.org/x/toolsv0.1.9
8+
github.com/stretchr/testifyv1.8.4
9+
golang.org/x/toolsv0.9.3
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spewv1.1.1// indirect
14+
github.com/pmezard/go-difflibv1.0.0// indirect
15+
golang.org/x/modv0.10.0// indirect
16+
golang.org/x/sysv0.8.0// indirect
17+
gopkg.in/yaml.v3v3.0.1// indirect
1018
)

‎go.sum‎

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,17 @@
1-
github.com/davecgh/go-spewv1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2-
github.com/davecgh/go-spewv1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1+
github.com/davecgh/go-spewv1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spewv1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/pmezard/go-difflibv1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
44
github.com/pmezard/go-difflibv1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/objxv0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6-
github.com/stretchr/testifyv1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7-
github.com/stretchr/testifyv1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8-
github.com/yuin/goldmarkv1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
9-
golang.org/x/cryptov0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
10-
golang.org/x/cryptov0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
11-
golang.org/x/modv0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
12-
golang.org/x/modv0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
13-
golang.org/x/netv0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
14-
golang.org/x/netv0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
15-
golang.org/x/netv0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
16-
golang.org/x/syncv0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
17-
golang.org/x/syncv0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
18-
golang.org/x/sysv0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
19-
golang.org/x/sysv0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20-
golang.org/x/sysv0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21-
golang.org/x/sysv0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22-
golang.org/x/sysv0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
23-
golang.org/x/sysv0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24-
golang.org/x/termv0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
25-
golang.org/x/textv0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
26-
golang.org/x/textv0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
27-
golang.org/x/textv0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
28-
golang.org/x/toolsv0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
29-
golang.org/x/toolsv0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
30-
golang.org/x/toolsv0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
31-
golang.org/x/toolsv0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
32-
golang.org/x/xerrorsv0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
33-
golang.org/x/xerrorsv0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
34-
golang.org/x/xerrorsv0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
35-
golang.org/x/xerrorsv0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5+
github.com/stretchr/testifyv1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testifyv1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
golang.org/x/modv0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
8+
golang.org/x/modv0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
9+
golang.org/x/syncv0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
10+
golang.org/x/sysv0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
11+
golang.org/x/sysv0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/toolsv0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
13+
golang.org/x/toolsv0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
3614
gopkg.in/check.v1v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3715
gopkg.in/check.v1v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38-
gopkg.in/yaml.v3v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
39-
gopkg.in/yaml.v3v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎lint_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package linters
33
// Tests for linters.
44

55
import (
6-
"github.com/stretchr/testify/suite"
76
"path/filepath"
87
"runtime"
98
"testing"
109

10+
"github.com/stretchr/testify/suite"
1111
"golang.org/x/tools/go/analysis/analysistest"
1212
)
1313

@@ -16,8 +16,7 @@ type linterSuite struct {
1616
}
1717

1818
func (suite*linterSuite)TestContextLinter() {
19-
analysistest.Run(
20-
suite.T(),TestdataDir(),
19+
analysistest.Run(suite.T(),TestdataDir(),
2120
TodoAnalyzer,"testlintdata/todo")
2221
}
2322

@@ -30,5 +29,6 @@ func TestdataDir() string {
3029
if!ok {
3130
panic("unable to get current test filename")
3231
}
32+
3333
returnfilepath.Join(filepath.Dir(testFilename),"testdata")
3434
}

‎plugin/example.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
package main
33

44
import (
5-
linters"github.com/dbraley/example-linter"
5+
linters"github.com/golangci/example-linter"
66
"golang.org/x/tools/go/analysis"
77
)
88

9+
// This must be defined and named 'AnalyzerPlugin'
10+
varAnalyzerPluginanalyzerPlugin
11+
912
typeanalyzerPluginstruct{}
1013

1114
// This must be implemented
1215
func (*analyzerPlugin)GetAnalyzers() []*analysis.Analyzer {
13-
return []*analysis.Analyzer{
14-
linters.TodoAnalyzer,
15-
}
16+
return []*analysis.Analyzer{linters.TodoAnalyzer}
1617
}
17-
18-
// This must be defined and named 'AnalyzerPlugin'
19-
varAnalyzerPluginanalyzerPlugin

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp