"Exported type should have comment or be unexported" golang VS Code

Who

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.


Why

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).


Solution

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:

enter image description here


How to change lint tool for the Go Extension in VS Code

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:

enter image description here

2) Select "golangci-lint" from the related drop-down list:

enter image description here

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:

enter image description here

enter image description here

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.


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.