例如,我正在开发留言簿。它允许添加记录,需要由管理员批准。但是,在开发进一步功能时,批准每个在测试期间添加的记录是很麻烦的。
那么,有没有可能构建应用程序的dev版本,以创建具有适当标志设置的这些记录呢?
例如,prod build编译了以下函数:
谢谢。
func NewRecord() Record {
return Record{Moderation: Awaiting}
}
在开发版本中,编译使用以下方法:
func NewRecord() Record {
return Record{Moderation: Approved}
}
我知道在前端开发中,当你构建一些JS应用时,设置NODE_ENV=production环境变量是生产环境常见的做法。 我正在寻找Go语言中类似的方法。 我看到两种方式,但都不喜欢:
- I can just set
Awaiting = Approved
while developing and then change it back to actual value when building prod version. But I afraid that one day I will forget about this mock, will commit it to repo or something like that. Change function to something like
func NewRecord() Record { if os.Getenv(mykey) == "production" { return Record{Moderation: Awaiting} } else { return Record{Moderation: Approved} } }
But I don't like that this condition is evaluated in runtime for each new record. It just seems to be wrong for compiled language.
谢谢。