go - "Exported type should have comment or be unexported"golang VS 代码

标签 go visual-studio-code golint

我在 Go 中尝试了这段代码:

type Agent struct {
    name string    // Not exported
    categoryId int // Not exported
}

VS Code 报告了以下问题:

exported type Agent should have comment or be unexported


警告有点烦人。所以我有以下问题:

  • 如何摆脱它?
  • 我应该发表什么评论?
  • 是否有任何默认评论模板?

它要求我发表评论,但默认情况下不让我添加评论。

最佳答案

只需在其上方添加注释,以您的类型(或函数、方法等)的名称开头,如下所示:

// Agent is ...
type Agent struct {
   name string
   categoryId int
}

此 linter 错误是由您的 Agent 类型导出引起的,即使它的属性没有导出。要不导出您的类型,请将其定义为小写形式:

type agent struct {
   name string
   categoryId int
}

你的 linter 提示这个的原因是 godoc 使用这些评论来自动为你的项目生成文档。您可以在 pkg.go.dev 找到许多此类记录在案的 Go 项目的示例。 .

例如,如果您将一个 Go 项目上传到 GitHub,pkg.go.dev 将使用这些评论自动为您生成一份文档。您甚至可以添加可运行的代码示例和许多其他内容,如 go-doc tricks 所示。 .

关于go - "Exported type should have comment or be unexported"golang VS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004291/

相关文章:

go - Apache Beam 从 Go 中的 PCollection 中选择前 N 行

go - 当通过另一个文件导入时,仅使用 ioutils ReadDir 扫描当前目录中的文件

go - 如何保护(混淆)Go 二进制文件不被破解

visual-studio-code - 是否有更改包含字符串的两个引号的快捷方式?

go - 如果 Ticker 已经在运行,则不应执行

linux - 如何向 VS Code 授予修改适用于 Linux 的 Windows 子系统中文件的权限

visual-studio-code - 如何排除多根工作区文件夹之一中的所有文件以在 Visual Studio 代码中进行搜索?

go - 在 travis 的 golang 仓库上运行 lint

go - 如何安装特定版本的 golint 以供全局使用?