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 關注
- 200 瀏覽
添加回答
舉報