当前位置:网站首页>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"边栏推荐
- Microbial safety and health, what is biological treatment?
- 编译安装oh-my-zsh
- About vctk datasets
- 【栈】921. Minimum Add to Make Parentheses Valid
- Mustache syntax
- 【模拟】922. Sort Array By Parity II
- Microbiological health, why is food microbiological testing important
- Visual studio 2019 Download
- flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
- 微生物安全與健康,什麼是生物處理?
猜你喜欢

Exploration and practice of "flow batch integration" in JD

Basic knowledge 3 - standard unit library

Dx-11q signal relay
![[learning notes] structure](/img/55/9623ba97f57eff71c246684e3a2bba.png)
[learning notes] structure

Pre training / transfer learning of models

"Open math input panel" in MathType editing in win11 is gray and cannot be edited

Q play soft large toast to bring more comfortable sleep

元宇宙为 VR/AR 带来的新机会

迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况

uniapp官方组件点击item无效,解决方案
随机推荐
[dynamic planning] path dp:931 Minimum Falling Path Sum
1175. Prime Arrangements
What are the functions of soil microorganisms in microbial detection?
Visual studio 2019 Download
Parity linked list [two general directions of linked list operation]
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
Why not two or four TCP handshakes
Introduction and principle analysis of cluster and LVS
What will Web3 bring in the future?
系统设置大页
用recyclerReview展示Banner,很简单
Open3d point cloud color rendering
Double position relay dls-5/2 dc220v
ESP8266 RC522
Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
Service grid ASM year end summary: how do end users use the service grid?
Dx-11q signal relay
neo4j安装、运行以及项目的构建和功能实现
uniapp官方组件点击item无效,解决方案
Applet Custom Grid