第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在檢查 Golang 類型時(shí)創(chuàng)建看起來(lái)像另一個(gè)的自定義類型

在檢查 Golang 類型時(shí)創(chuàng)建看起來(lái)像另一個(gè)的自定義類型

Go
qq_笑_17 2021-12-07 18:37:46
我是一名經(jīng)驗(yàn)豐富的 Python 程序員,但我對(duì) Golang 還是個(gè)新手,所以如果這是一個(gè)明顯或愚蠢的問(wèn)題,我深表歉意。但是我正在嘗試創(chuàng)建我自己的類型,我希望它的行為與基類型完全相同,但有幾個(gè)方法被覆蓋。原因是因?yàn)槲沂褂玫膸讉€(gè)庫(kù)正在檢查類型time.Time,我希望它匹配。type PythonTime struct {    time.Time}var pythonTimeFormatStr = "2006-01-02 15:04:05-0700"func (self *PythonTime) UnmarshalJSON(b []byte) (err error) {    // removes prepending/trailing " in the string    if b[0] == '"' && b[len(b)-1] == '"' {        b = b[1 : len(b)-1]    }    self.Time, err = time.Parse(pythonTimeFormatStr, string(b))    return}func (self *PythonTime) MarshalJSON() ([]byte, error) {    return []byte(self.Time.Format(pythonTimeFormatStr)), nil}type OtherType struct {    Uuid     string     `json:"uuid`    Second   PythonTime `json:"second"`    Location string     `json:"location"`    Action   string     `json:"action"`    Duration int        `json:"duration"`    Value    string     `json:"value"`}因此,上述內(nèi)容適用于編組和解組 JSON。但是,對(duì)于我正在使用的庫(kù)(gocql 和 cqlr),他們正在檢查類型是否為time.Time類型,以便他們可以在將其放入 C* 之前進(jìn)行一些其他修改。我如何讓我的PythonTime類型等同于顯示time.Time 或覆蓋對(duì)象的默認(rèn)編組/解組,time.Time只是為了使用我的OtherType對(duì)象?我的臨時(shí)解決方案是修改他們的代碼,并為與PythonTime類型做同樣事情的time.Time類型添加一個(gè)特例。但是,這導(dǎo)致我循環(huán)導(dǎo)入并且不是最佳解決方案。這是我修改后的代碼。func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) {    switch v := value.(type) {    case Marshaler:        return v.MarshalCQL(info)    case int64:        return encBigInt(v), nil    case time.Time:        if v.IsZero() {            return []byte{}, nil        }        x := int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6)        return encBigInt(x), nil    case models.PythonTime:        x := int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6)        return encBigInt(x), nil    }    if value == nil {        return nil, nil    }    rv := reflect.ValueOf(value)    switch rv.Type().Kind() {    case reflect.Int64:        return encBigInt(rv.Int()), nil    }    return nil, marshalErrorf("can not marshal %T into %s", value, info)}
查看完整描述

3 回答

?
揚(yáng)帆大魚(yú)

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

不要這樣做。time.Time當(dāng)您應(yīng)該檢查對(duì)象是否滿足接口時(shí),您正在檢查對(duì)象。


type TimeLike interface {

    Day() int

    Format(string) string

    ...  // whatever makes a "time" object to your code!

    // looks like in this case it's

    UTC() time.Time

    IsZero() bool

}

那么任何需要 a 的代碼time.Time都可以用 a 替換,而是PythonTime期待 a TimeLike。


function Foo(value interface{}) int {

    switch v := value.(type) {

    case TimeLike:

        return v.Day()  // works for either time.Time or models.PythonTime

    }

    return 0

}


查看完整回答
反對(duì) 回復(fù) 2021-12-07
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

就像使用json.Marshalerand 一樣json.Unamrshaler,您也可以實(shí)現(xiàn)接口。gocql.Marshaler gocql.Unamrshaler


func (t *PythonTime) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {

    b := make([]byte, 8)

    x := t.UnixNano() / int64(time.Millisecond)

    binary.BigEndian.PutUint64(b, uint64(x))

    return b, nil

}


func (t *PythonTime) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {

    x := int64(binary.BigEndian.Uint64(data)) * int64(time.Millisecond)

    t.Time = time.Unix(0, x)

    return nil

}

(注意,在 CQL 的上下文中未經(jīng)測(cè)試,但這會(huì)與自身進(jìn)行往返)


查看完整回答
反對(duì) 回復(fù) 2021-12-07
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

不幸的是,這在 Go 中不起作用。您最好的選擇是創(chuàng)建一些導(dǎo)入和導(dǎo)出方法,以便您可以將 PythonTime 轉(zhuǎn)換為 time.Time,反之亦然。這將為您提供所需的靈活性以及與其他庫(kù)的兼容性。


package main


import (


    "fmt"

    "reflect"

    "time"


)


func main() {


    p, e := NewFromTime(time.Now())

    if e != nil {

        panic(e)

    }


    v, e := p.MarshalJSON()

    if e != nil {

         panic(e)

    }


    fmt.Println(string(v), reflect.TypeOf(p))


    t, e := p.GetTime()

    if e != nil {

        panic(e)

    }


    fmt.Println(t.String(), reflect.TypeOf(t))


}


type PythonTime struct {

    time.Time

}


var pythonTimeFormatStr = "2006-01-02 15:04:05-0700"


func NewFromTime(t time.Time) (*PythonTime, error) {


b, e := t.GobEncode()

if e != nil {

    return nil, e

}


p := new(PythonTime)

e = p.GobDecode(b)

if e != nil {

    return nil, e

}


    return p, nil

}


func (self *PythonTime) GetTime() (time.Time, error) {

    return time.Parse(pythonTimeFormatStr, self.Format(pythonTimeFormatStr))

}


func (self *PythonTime) UnmarshalJSON(b []byte) (err error) {

    // removes prepending/trailing " in the string

    if b[0] == '"' && b[len(b)-1] == '"' {

        b = b[1 : len(b)-1]

    }

    self.Time, err = time.Parse(pythonTimeFormatStr, string(b))

    return

}


func (self *PythonTime) MarshalJSON() ([]byte, error) {

    return []byte(self.Time.Format(pythonTimeFormatStr)), nil

}

那應(yīng)該給出這樣的輸出:


2016-02-04 14:32:17-0700 *main.PythonTime


2016-02-04 14:32:17 -0700 MST time.Time


查看完整回答
反對(duì) 回復(fù) 2021-12-07
  • 3 回答
  • 0 關(guān)注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)