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

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

如何在 Go 中表示 PostgreSQL 區間

如何在 Go 中表示 PostgreSQL 區間

Go
江戶川亂折騰 2021-11-08 18:18:09
如何在 Go 中表示 PostgreSQL 區間?我的結構是這樣的:type Product struct {    Id              int    Name            string    Type            int    Price           float64    Execution_time  ????}我的數據庫上的 execution_time 字段是interval.
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

最佳答案我已經遇到是使用bigint你的架構,并實現Value與Scan上一個包裝類型time.Duration。


// Duration lets us convert between a bigint in Postgres and time.Duration

// in Go

type Duration time.Duration


// Value converts Duration to a primitive value ready to written to a database.

func (d Duration) Value() (driver.Value, error) {

    return driver.Value(int64(d)), nil

}


// Scan reads a Duration value from database driver type.

func (d *Duration) Scan(raw interface{}) error {

    switch v := raw.(type) {

    case int64:

        *d = Duration(v)

    case nil:

        *d = Duration(0)

    default:

        return fmt.Errorf("cannot sql.Scan() strfmt.Duration from: %#v", v)

    }

    return nil

}

不幸的是,您將犧牲在查詢中進行區間算術的能力 - 除非一些聰明的人想要發布bigint=>的類型轉換interval。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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