【发布时间】:2019-10-17 10:29:11
【问题描述】:
我想要某种“挂钩”,只要我从数据库中获取特定类型的对象,它就会运行。我认为Unmarshaler 接口非常适合,但是...如何在不自己手动解组每个字段的情况下实现该接口?
我想过做这样的事情:
func (t *T) UnmarshalBSON(b []byte) error {
// Simply unmarshal `b` into `t` like it would otherwise
bson.Unmarshal(b, t) // Obviously this won't work, it'll be an infinite loop
// Do something here
return nil
}
如何在不使用反射 pkg 手动解组字段的情况下实现此目的?
【问题讨论】:
-
不确定这是否是最好的方法,但它会工作gist.github.com/miguelmota/…。