目录
学习笔记,写到哪是哪。
接着上一篇文章:Go语学习笔记 - 增加文件下载工具 | Web框架Gin(六)_的博客-CSDN博客
我已经把暂时想到的工具类补充好了,开始数据库的连接和使用。
从这篇开始会慢慢在增加数据库的相关操作。
项目地址:github地址
依赖安装
首先安装一下所需要的gorm和mysql驱动。
安装指令如下:
go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql
Mmysql配置
修改配置文件toml_config.go,增加mysql相关配置。
具体的使用方式参考:Go语学习笔记 - 配置文件使用、日志配置 | Web框架Gin(二)_的博客-CSDN博客_config.toml
toml_config.go文件调整后代码如下:
package toml
import (
"fmt"
"github.com/spf13/viper"
)
type TomlConfig struct {
AppName string
Log LogConfig
Mysql MysqlConfig
}
// 日志保存地址
type LogConfig struct {
Path string
Level string
}
// 日志保存地址
type MysqlConfig struct {
Host string
User string
Password string
DbName string
Port int64
}
var c TomlConfig
func init() {
// 设置文件名