mysql中date/datetime类型自动转go的时间类型time.Time

在DSN中需要加入parseTime=true&&loc=Local,或
charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true

go 复制代码
package main_test

import (
	"database/sql"
	"fmt"
	"testing"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

func TestMysqlDatetime(t *testing.T) {

	type Student struct {
		CreateAt time.Time
	}

	db, _ := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/test?parseTime=true&&loc=Local")
	rows, _ := db.Query("SELECT create_at FROM `student`")
	defer rows.Close()
	var student Student
	for rows.Next() {
		err := rows.Scan(&student.CreateAt)
		if err != nil {
			fmt.Printf("scan failed, err:%v\n", err)
			return
		}
		fmt.Println("student.CreateAt:", student.CreateAt.Format("2006-01-02 15:04:05"))
	}
}

参考

https://github.com/go-sql-driver/mysql#timetime-support

相关推荐
t***442310 小时前
MySQL 导出数据
数据库·mysql·adb
翔云12345610 小时前
MySQL主从库复制中,主库如何查找对应日志文件位置
数据库·mysql
Mr_star_galaxy11 小时前
【MySQL基础】视图和权限管理
数据库·mysql
lipiaoshuigood12 小时前
MySQL 数据出海之数据同步方案
数据库·mysql
笨蛋不要掉眼泪13 小时前
Nacos配置中心详解:核心用法、动态刷新与经典面试题解析
java·数据库·后端
@@神农13 小时前
PostgreSQL-SQL语句的执行过程(一)
数据库·sql·postgresql
Andy Dennis14 小时前
一文漫谈数据库存储之索引(B+, B-link, LSM tree等)
数据库·b+树·lsm-tree
CHANG_THE_WORLD15 小时前
字符串定义的汇编分析
汇编·数据库
数据知道15 小时前
PostgreSQL:如何通过progres_fdw跨库关联查询?
数据库·postgresql
v***570015 小时前
MYSQL 创建索引
数据库·mysql