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

GoVisual Studio-CodeGolint

Go Problem Overview


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:

  • How to get rid of it?
  • What comment should I put?
  • Is there any default comment template for this?

It asks me to put a comment but it does not offer me to add one by default.

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

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.

Solution 3 - Go

You can fix this issue by simply adding comment above the function name.

Example below:

//ArrayBasic1 is the function for calculating array
func ArrayBasic1() {
  x := [5]int{1: 10, 2: 20, 3: 30}
  fmt.Println("value from arraybasic1", x)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchanna lyView Question on Stackoverflow
Solution 1 - GoUllaakutView Answer on Stackoverflow
Solution 2 - Goinformatik01View Answer on Stackoverflow
Solution 3 - GoBuddheshwarView Answer on Stackoverflow