我想通过 Go 将用户 checkin 和 checkout 时间(小时、分钟和秒)存储在 Postgres 表中(列类型当然是 time)
query := `INSERT INTO oc_log (userid, userdate, checkin_time, checkin_location, checkout_time, checkout_location) VALUES ($1, $2, $3, $4, $5, $6)`
err := db.QueryRow(query, userid, time.Now().Format("01-02-2006"), time.Now().Clock(), checkinlocation, "", "").Scan(&tmpid)
go linter 提示说
multiple-value time.Now().Clock() in single-value context
如何解决这个问题?
最佳答案
只需使用time.Time
值,让驱动程序担心如何编码和解码它们:
package main
import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "")
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("CREATE TABLE foo (t timetz)")
if err != nil {
log.Fatal(err)
}
now := time.Now()
_, err = db.Exec("INSERT INTO foo VALUES ($1)", now)
if err != nil {
log.Fatal(err)
}
var t time.Time
err = db.QueryRow("SELECT t FROM foo").Scan(&t)
if err != nil {
log.Fatal(err)
}
fmt.Println(now.Format(time.RFC3339)) // 2019-09-18T10:22:36+02:00
fmt.Println(t.Format(time.RFC3339)) // 0000-01-01T10:22:36+02:00
}
关于postgresql - 如何在 Postgres 表上存储 Go 的当前时间(小时-分钟-秒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57987718/