musttag
v0.13.0
在(UN)填充结构中强制实行字段标签的GO linter。
musttag检查将传递给Marshal功能的结构的导出字段带有相关标签注释:
// BAD:
var user struct {
Name string
}
data , err := json . Marshal ( user )
// GOOD:
var user struct {
Name string `json:"name"`
}
data , err := json . Marshal ( user )Uber风格指南的理性:
结构的序列化形式是不同系统之间的合同。更改序列化形式的结构(包括字段名称)破坏了此合同。在标签中指定字段名称可以明确说明合同,并防止通过重构或重命名字段意外违反合同。
开箱即用的支持以下软件包:
此外,任何自定义软件包都可以添加到列表中。
musttag集成到golangci-lint中,这是建议使用它的方法。
要启用Linter,请将以下行添加到.golangci.yml :
linters :
enable :
- musttag另外,您可以从“版本”页面下载预先构建的二进制文件以使用musttag独立。
在启用musttag的情况下,运行golangci-lint 。请参阅配置Linter的可用选项列表。
使用musttag独立时,将选项作为标志传递。
要报告自定义功能,您需要将其描述添加到.golangci.yml 。以下是添加对hclsimple.Decode的支持的示例:
linters-settings :
musttag :
functions :
# The full name of the function, including the package.
- name : github.com/hashicorp/hcl/v2/hclsimple.Decode
# The struct tag whose presence should be ensured.
tag : hcl
# The position of the argument to check.
arg-pos : 2可以通过-fn=<name:tag:arg-pos> musttag :
musttag -fn= " github.com/hashicorp/hcl/v2/hclsimple.DecodeFile:hcl:2 " ./...