2 回答
TA贡献1876条经验 获得超6个赞
你的代码有一个错字。您不能取消引用非指针,因此您需要使 GetBSON 成为指针接收器(或者您可以删除指向 的间接对象t,因为 的值t不会被该方法更改)。
func (t *Timestamp) GetBSON() (interface{}, error) {
要设置*Timestamp内联值,您需要有一个*time.Time要转换的。
now := time.Now()
u := User{
Name: "Bob",
CreatedAt: (*Timestamp)(&now),
}
构造函数和辅助函数就像这样New(),Now()也可能会派上用场。
TA贡献1813条经验 获得超2个赞
您不能引用不是指针变量的东西的间接引用。
var a int = 3 // a = 3
var A *int = &a // A = 0x10436184
fmt.Println(*A == a) // true, both equals 3
fmt.Println(*&a == a) // true, both equals 3
fmt.Println(*a) // invalid indirect of a (type int)
因此,您不能引用awith的地址*a。
查看错误发生的位置:
func (t Timestamp) GetBSON() (interface{}, error) {
// t is a variable type Timestamp, not type *Timestamp (pointer)
// so this is not possible at all, unless t is a pointer variable
// and you're trying to dereference it to get the Timestamp value
if time.Time(*t).IsZero() {
return nil, nil
}
// so is this
return time.Time(*t), nil
}
- 2 回答
- 0 关注
- 168 浏览
添加回答
举报