I tried this code in Go:
type Agent struct {
name string // Not exported
categoryId int // Not exported
}
And VS Code reports the following problem:
exported type Agent should have comment or be unexported
The warning is kind of annoying. So I have the following questions:
It asks me to put a comment but it does not offer me to add one by default.
Just add a comment above it, starting with the name of your type (or function, method etc.) like this:
// Agent is ...
type Agent struct {
name string
categoryId int
}
This linter error is caused by your Agent
type being exported, even if its attributes are not. To not export your type, define it in lowercase like such:
type agent struct {
name string
categoryId int
}
The reason why your linter complains about this is that godoc uses those comments to automatically generate documentation for your projects. You can find many examples of such documented Go projects at pkg.go.dev.
If you upload one of your Go projects to GitHub for example, pkg.go.dev will automatically generate a documentation for you using those comments. You can even add runnable code examples and many other things, as shown on go-doc tricks.
This warning is produced by the official linter for Go source code - golint. Golint is used as the default linter by the Go extension in Visual Studio Code editor.
To understand the reason why golint showed that warning, we can refer to the "Commentary" section in the "Effective Go" (the official document that gives tips for writing clear, idiomatic Go code). Here is the related quote:
Every exported (capitalized) name in a program should have a doc comment.
But this indeed can be annoying if you tend to write self-documenting code (i.e. the intention is clear from a name itself etc).
Besides the already proposed solutions, what you can do is start using the alternative and more advanced golangci-lint which is a Go linters aggregator. It has golint disabled by default, so this annoying warning about missing doc comments will not be triggered. Of course you can turn this warning on if you want by using the related flags (see --exclude strings
and --exclude-use-default
).
The possibility to change the linter is also mentioned on the description page of Go extension:
In order to change lint tool in VS Code perform the following steps.
1) Choose the "Configure Extension Settings" in the Go Extension's management menu:
2) Select "golangci-lint" from the related drop-down list:
3) To prevent VS Code from freezing as a result of using this powerful linter, add the --fast
flag as described in the "Editor Integration" instructions.
To do that, you need to navigate to the Go Extension configuration page (as in step 1), open the settings.json
file and add the related configuration as shown on the below screenshots:
NB! Here is a quote from the "golangci-lint" FAQ:
Why running with --fast is slow on the first run?
Because the first run caches type information. All subsequent runs will be fast. Usually this options is used during development on local machine and compilation was already performed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With