当前位置:网站首页>Gin configuration file
Gin configuration file
2022-07-01 01:31:00 【weixin_ thirty-eight million one hundred and seven thousand fou】

One 、 Configuration information :mysql.json
{
"host":"localhost",
"port":"3306",
"user_name":"root",
"password":"Qazwsx123",
"database":"gin_project",
"logo_mode":true
}Two 、 Load profile
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
}
3、 ... and 、 Use configuration information
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) // maximum connection
Db.DB().SetMaxIdleConns(50) // Maximum free number
Db.AutoMigrate(&models.User{})
}Four Use stay main.go introduce
_ "gin_project/data_source"边栏推荐
- 面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
- Strictmode jamming and leakage detection -strictmode principle (2)
- Basic knowledge II - Basic definitions related to sta
- Dx-11q signal relay
- 元宇宙为 VR/AR 带来的新机会
- 用 Flutter 的 Canvas 画点有趣的图形
- 流批一体在京东的探索与实践
- Q play soft large toast to bring more comfortable sleep
- mysql插入\更新前+判断条件
- [leetcode] sum of two numbers [1]
猜你喜欢

Service grid ASM year end summary: how do end users use the service grid?
![[learning notes] structure](/img/55/9623ba97f57eff71c246684e3a2bba.png)
[learning notes] structure
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]

微研所,微生物检验中常用的生化反应

Docker deployment MySQL 8

Poor students can also play raspberry pie
![[learning notes] double + two points](/img/d4/1ef449e3ef326a91966da11b3c8210.png)
[learning notes] double + two points

1500w播放下还藏着什么热点?B站2个未来趋势你不得错过

DC学习笔记正式篇之零——综述与基本流程介绍

【Qt5-基础篇】随机数显示屏展示
随机推荐
【栈】921. Minimum Add to Make Parentheses Valid
元宇宙为 VR/AR 带来的新机会
About vctk datasets
sort自定义函数
Why build a personal blog
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
微生物健康,食品微生物检测为什么很重要
迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况
Orb-slam2 source code learning (II) map initialization
基础知识之二——STA相关的基本定义
[deepin] common sets
Pre training / transfer learning of models
06. on several ways of redis persistence
Dx-11q signal relay
二季度最后一天
【模拟】922. Sort Array By Parity II
None of the following candidates is applicable because of a receiver type mismatch
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
Service grid ASM year end summary: how do end users use the service grid?
Digital IC design process summary