【发布时间】:2022-01-09 17:49:56
【问题描述】:
我在不使用 ORM 的情况下实现了一些数据库逻辑。
我能以某种方式在结构键和数据库枚举之间创建映射吗?
type Message struct {
SomeKey string
SomeOtherKey string
}
MessageToDBEnum: = map[ ? MessageKey] string {
SomeKey: "some_key",
SomeOtherKey: "some_other_key"
}
- 我以后可以动态地使用映射键(例如在遍历映射时)来设置/获取结构值吗?
- 我还能以某种方式确保
MessageToDBEnum是详尽无遗的(包括 Message 的所有公钥)吗?
【问题讨论】:
-
据我所知,可以使用pkg.go.dev/github.com/mitchellh/mapstructure 实现转换,但我无法直接控制消息(它由其他包公开)
标签: dictionary go struct enums