英文:
How to have a Development struct and Production struct in Golang with identical members but different JSON tags?
问题
第一次提问!
我正在尝试将使用相同结构的开发和生产分开。
我正在使用Airtable,它将记录作为带有fld标签的JSON发送,我们在解组时使用该标签。
type AirtableRecord struct {
Name *string `json:"fldAAAA,omitempty"`
}
我有两个独立的Airtable:
- 用于开发
- 用于生产
它们是相同的,只是由于Airtable的工作方式,字段被赋予不同的fld标签。
现在,为了将开发环境与生产环境分开,我必须根据指向的Airtable取消注释正确的成员。
type AirtableRecord struct {
// 开发环境
Name *string `json:"fldAAAA,omitempty"`
// 生产环境
//Name *string `json:"fldBBBB,omitempty"`
}
我将这个类型保存在自己的model.go文件中,其他包会使用它。
我已经研究过:
- 在一行中有多个JSON标签,Golang不支持这样做
type AirtableRecord struct {
// 开发环境或生产环境
Name *string `json:"fldAAAA,fldBBBB,omitempty"`
}
- 使用构建标签分离我的文件,也许这样可以,但我做错了
文件1:
// +build dev
type AirtableRecord struct {
// 开发环境
Name *string `json:"fldAAAA,omitempty"`
}
文件2:
type AirtableRecord struct {
// 生产环境
Name *string `json:"fldBBBB,omitempty"`
}
- 研究了使用retag,但他们给出的示例不符合我的需求
- retag链接:https://pkg.go.dev/github.com/sevlyar/retag@v0.0.0-20190429052747-c3f10e304082
我想根据我是在开发模式还是生产模式下运行来动态更改这个成员的标签。
非常感谢任何帮助!
英文:
First time asking a question!
I am trying to separate my development and production that use the same struct.
I am working with Airtable, which sends records as a JSON with an fld tag that we use when unmarshaling.
type AirtableRecord struct {
Name *string `json:"fldAAAA,omitempty"`
}
I have 2 separate Airtables:
- For development
- For production
They are identical, except because of how Airtable works the fields are given different fld tags
Right now to separate development env from production env, I have to uncomment the right members depending which Airtable I am pointing to.
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
// Production
//Name *string `json:"fldBBBB,omitempty"`
}
I keep this type in it's own model.go file, that other packages use.
I have looked into:
- Having multiple JSON tags in a line, Golang doesn't do this
type AirtableRecord struct {
// Development or Production
Name *string `json:"fldAAAA,fldBBBB,omitempty"`
}
- Separating my files using build tags, maybe this works but I'm doing it wrong
File 1:
// +build dev
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
}
File 2:
type AirtableRecord struct {
// Production
Name *string `json:"fldBBBB,omitempty"`
}
- Looked into using retag, but the examples they gave don't look like what I'm looking for
I want to change this Member's tag dynamically based on if I'm running in development mode or production mode.
Any and all help would be appreciated!
答案1
得分: 2
如果在使用构建标签时遇到“在此块中重新声明”编译错误,请在生产文件中指定一个非“dev”的标签,以避免此错误。
开发文件:
// +build dev
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
}
生产文件:
// +build !dev
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
}
构建命令:
# 对于开发环境
go build -tags=dev -o devrel
# 对于生产环境
go build -tags=prod -o prodrel
或者对于生产环境不使用标签
另外,自1.17版本以来,构建标签的格式已更改,所以在您的情况下,应该是:
//go:build dev
但旧的格式也应该可以工作。
英文:
If you are getting redeclared in this block
compile error with build tags, then specify a not'ed tag on prod file, to avoid it.
Dev File
// +build dev
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
}
Prod File
// +build !dev
type AirtableRecord struct {
// Development
Name *string `json:"fldAAAA,omitempty"`
}
build with
# for dev
go build -tags=dev -o devrel
# For prod
go build -tags=prod -o prodrel
Or no tags for prod
Also build tag format has changed since 1.17 so in your case it will be,
//go:build dev
but should work with old one too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论