亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 中的時間戳

Golang 中的時間戳

Go
呼如林 2021-11-01 15:13:15
試圖在我的應用程序中使用這種時間戳方法:https : //gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go這里是:package timestampimport (    "fmt"    "labix.org/v2/mgo/bson"    "strconv"    "time")type Timestamp time.Timefunc (t *Timestamp) MarshalJSON() ([]byte, error) {    ts := time.Time(*t).Unix()    stamp := fmt.Sprint(ts)    return []byte(stamp), nil}func (t *Timestamp) UnmarshalJSON(b []byte) error {    ts, err := strconv.Atoi(string(b))    if err != nil {        return err    }    *t = Timestamp(time.Unix(int64(ts), 0))    return nil}func (t Timestamp) GetBSON() (interface{}, error) {    if time.Time(*t).IsZero() {        return nil, nil    }    return time.Time(*t), nil}func (t *Timestamp) SetBSON(raw bson.Raw) error {    var tm time.Time    if err := raw.Unmarshal(&tm); err != nil {        return err    }    *t = Timestamp(tm)    return nil}func (t *Timestamp) String() string {    return time.Time(*t).String()}以及與之相關的文章:https : //medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f但是,我收到以下錯誤:core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)                                                                                                                                                     core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)我的相關代碼如下所示:import (    "github.com/path/to/timestamp")type User struct {    Name        string    Created_at  *timestamp.Timestamp  `bson:"created_at,omitempty" json:"created_at,omitempty"`} 誰能看到我做錯了什么?相關問題 我也看不到如何實現這個包。我是否創建了這樣的新用戶模型?u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
查看完整描述

2 回答

?
HUX布斯

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()也可能會派上用場。


查看完整回答
反對 回復 2021-11-01
?
慕姐8265434

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

}


查看完整回答
反對 回復 2021-11-01
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號