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 " ./...