我在 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/