

golang gorm mysql 读写时间戳
source link: https://studygolang.com/articles/35245
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

golang gorm mysql 读写时间戳
eflight · 2天之前 · 100 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览gorm直接写time.Time类型时,会报错
在打开数据库命令中加入parseTime=true&loc=Local
例如:root:1234567@tcp(127.0.0.1:3306)/dbName?charset=utf8&parseTime=true&loc=Local&timeout=1000ms
自定义自己的time类型,用于数据库读写
package types
import (
"database/sql/driver"
"fmt"
"time"
)
// Time is alias type for time.Time
type Time time.Time
const (
timeFormart = "2006-01-02 15:04:05"
zone = "Asia/Shanghai"
)
// UnmarshalJSON implements json unmarshal interface.
func (t *Time) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
*t = Time(now)
return
}
// MarshalJSON implements json marshal interface.
func (t Time) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(timeFormart)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, timeFormart)
b = append(b, '"')
return b, nil
}
func (t Time) String() string {
return time.Time(t).Format(timeFormart)
}
func (t Time) local() time.Time {
loc, _ := time.LoadLocation(zone)
return time.Time(t).In(loc)
}
// Value ...
func (t Time) Value() (driver.Value, error) {
var zeroTime time.Time
var ti = time.Time(t)
if ti.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return ti, nil
}
// Scan valueof time.Time 注意是指针类型 method
func (t *Time) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = Time(value)
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
原理 自定义数据库数据类型,在 sql driver 中实现自定义类型需要实现 Scanner和Valuer接口
Scanner
type Scanner interface {
Scan(src interface{}) error
}
Valuer
type Valuer interface {
// Value returns a driver Value.
Value() (Value, error)
}
unmarshal和marshal 自定义 json 转换格式
本文用作学习笔记,参考转载了 gorm 处理时间戳 的内容,如若侵权,请联系删除。
Recommend
-
234
jinzhu/gorm: GORM V1, V2 moved to https://github.com/go-gorm/gorm...
-
45
golang 官方的那个操作mysql的有点麻烦所以就使用了gorm,下面就gorm的使用做下简单介绍 下载gorm: go get -u github.com/jinzhu/gorm 在项目中引入gorm: import ( "github.com/jinzhu/...
-
41
前言 对于golang中使用gorm操作mysql,可能其他的操作都是固定的,唯一麻烦的就是字段的映射。 gorm允许自己定义一个表的结构映射,但是,golang中,首字母大写来表示public或者private,而gorm在做映射的时候,大写...
-
29
gorm是go语言的一个orm框架,框架的原理和思想在这里就不做详细介绍了,我主要演示一下gorm的实际使用。 开启mysql连接 主要用到 gorm.open()这个方法 //参数含义:数据库用户名、密码、主...
-
8
The fantastic ORM library for Golang, aims to be developer friendly. Overview Full-Featured ORM Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance) Hooks (B...
-
5
<?xml encoding="utf-8" ??>Introduction This guide explains how to build an example web API in Go to create, update, delete book records from a database. At the end of this article, you should be...
-
9
V2EX › 程序员 Golang gorm 怎么跨库查询 MySQL? gejigeji · 1 天前 · 1096 次...
-
5
Simple paging with Golang templates and GORM Published: Jul 14, 2020 / Last modified: Jul 15, 2020 Go code: const PageCount = 10 type P...
-
12
goshop开源项目的更新 备注:前面项目中用到的代码已经分享到GitHub中去了,并且以后所有项目中会出现的代码都会提交上去,欢迎查阅。感兴趣的可以点个star哦~
-
7
golang gorm框架的sql注入漏洞 keluda · 2020-06-27 18:32:50 · 6153 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK