当前位置:网站首页>gin 配置文件
gin 配置文件
2022-07-01 00:41:00 【weixin_38107457】

一、配置信息:mysql.json
{
"host":"localhost",
"port":"3306",
"user_name":"root",
"password":"Qazwsx123",
"database":"gin_project",
"logo_mode":true
}二、加载配置文件
package data_source
import (
"encoding/json"
"io/ioutil"
"os"
)
type MysqlConf struct {
Host string `json:"host"`
Port string `json:"port"`
UserName string `json:"user_name"`
Password string `json:"password"`
DataBase string `json:"database"`
LogoMode bool `json:"logo_mode"`
}
func LoadMysqlConf() *MysqlConf {
mysql_conf := MysqlConf{}
file, err := os.Open("conf/mysql_conf.json")
if err != nil {
panic(err)
}
defer file.Close()
byte_data, err2 := ioutil.ReadAll(file)
if err2 != nil {
panic(err2)
}
err3 := json.Unmarshal(byte_data, &mysql_conf)
if err3 != nil {
panic(err3)
}
return &mysql_conf
}
三、使用配置信息
package data_source
import (
"github.com/jinzhu/gorm"
"gin_project/models"
_ "github.com/jinzhu/gorm/dialects/mysql"
"fmt"
)
var Db *gorm.DB
var err error
func init() {
mysql_conf := LoadMysqlConf()
logo_mode := mysql_conf.LogoMode
data_source := fmt.Sprintf("%s:%[email protected](%s:%s)/%s?charset=utf8&parseTime=true&loc=Local",
mysql_conf.UserName,
mysql_conf.Password,
mysql_conf.Host,
mysql_conf.Port,
mysql_conf.DataBase,
)
Db,err = gorm.Open("mysql",data_source)
if err != nil {
panic(err)
}
Db.LogMode(logo_mode)
Db.DB().SetMaxOpenConns(100) // 最大连接数
Db.DB().SetMaxIdleConns(50) // 最大空闲数
Db.AutoMigrate(&models.User{})
}四 使用 在main.go 引入
_ "gin_project/data_source"边栏推荐
猜你喜欢

用recyclerReview展示Banner,很简单
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]

【网络丢包,网络延迟?这款神器帮你搞定所有!】

WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑

探索互联网时代STEAM教育创新之路

Q play soft large toast to bring more comfortable sleep

Exploring the road of steam education innovation in the Internet Era

fluttertoast

Green, green the reed. dew and frost gleam.

双位置继电器DLS-5/2 DC220V
随机推荐
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
解读创客教育所蕴含的科技素养
Windows环境下安装MongoDB数据库
System.CommandLine版CSRebot
流批一体在京东的探索与实践
fluttertoast
Open3D 点云颜色渲染
用recyclerReview展示Banner,很简单
pytorch编程知识(2)
一些本质的区别
Two position relay st2-2l/ac220v
机器人编程的培训学科类原理
系统设置大页
分割链表[先取next再斩断链表防止断链]
自定义注解实现校验
06.论Redis持久化的几种方式
基础知识之一——STA基础概述
Typora的使用
(learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images