我知道我们不能在Go中将struct用作常量。
但是我想将我的常量归为更干净的代码,而不是使用一些带有一些前缀的常量,所以我这样做:
var SomeStatus = struct {
Active int
Inactive int
Others int
}{
Active: 1,
Inactive: 2,
Others: 3
}
// usage example
status = Status.Active
// example with some prefix
const StatusActive = 1
const StatusInactive = 2
const StatusOthers = 3
const OtherConstantVariable = 1
...
如果没关系,可以重写变量的值。此技巧的其他副作用是什么?
最佳答案
此技巧的其他副作用是什么?
我能想到的。可能还有其他:
关于go - 使用结构对常量进行分组有哪些副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62898038/