当前位置: 首页 > news >正文

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

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

package main_testimport ("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 Studentfor 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

http://www.lryc.cn/news/196595.html

相关文章:

  • MATLAB算法实战应用案例精讲-【图像处理】机器视觉(基础篇)
  • LDAP协议工作原理
  • 【Jetpack Compose】BOM是什么?
  • 多域名SSL数字证书是什么呢
  • 杭电oj--求奇数的乘积
  • E053-web安全应用-Brute force暴力破解初级
  • 外汇天眼;VT Markets 赞助玛莎拉蒂MSG Racing电动方程式世界锦标赛
  • 使用vscode + vite + vue3+ element3 搭建vue3脚手架
  • 竞赛 深度学习+opencv+python实现车道线检测 - 自动驾驶
  • spring boot 下载resources下的静态文件为流格式
  • HTML渲染过程
  • [已解决]llegal target for variable annotation
  • nodejs基于vue小型企业银行账目管理系统
  • pointnet和pointnet++点云分割和分类
  • Docker-compose和Consul
  • AFL模糊测试+GCOV覆盖率分析
  • leetcode 965.单值二叉树
  • 云计算:掌控未来,一触即发!
  • Mybatis对数据库进行增删查改以及单元测试
  • .bat 批处理 - 查看 MySQL 状态然后启动或关闭
  • 跳转传参有几种方式
  • DVWA靶场Medium难度部分解析
  • SVG图形
  • 冒泡排序和简答选择排序
  • leetcode3. 无重复字符的最长子串 [滑动窗口]
  • 软件工程与计算总结(十六)详细设计的设计模式
  • List集合拆分为多个List
  • Hadoop3教程(十三):MapReduce中的分区
  • 笔记本Win10系统一键重装操作方法
  • FilterRegistrationBean能不能排除指定url