当前位置:网站首页>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"边栏推荐
- Chromatic judgement bipartite graph
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- [learning notes] structure
- Mustache syntax
- Koa koa combine routes sub route management
- 关于VCTK数据集
- Digital IC design process summary
- (learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
- 做生意更加务实
- Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
猜你喜欢

Installing mongodb database in Windows Environment

Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support

Typora的使用

解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘

蒹葭苍苍,白露为霜。

Kongyiji's first question: how much do you know about service communication?
![[learning notes] double + two points](/img/d4/1ef449e3ef326a91966da11b3c8210.png)
[learning notes] double + two points

The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?

图的连通性基础

Green, green the reed. dew and frost gleam.
随机推荐
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
Parity linked list [two general directions of linked list operation]
Locking relay ydb-100, 100V
【go】go 实现行专列 将集合进行转列
None of the following candidates is applicable because of a receiver type mismatch
Analyze the maker education path integrating the essence of discipline
基础知识之一——STA基础概述
[LeetCode] 两数之和【1】
C# 自定义并动态切换光标
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
Training discipline principle of robot programming
为什么要搭建个人博客
Introduction and principle analysis of cluster and LVS
Web interface testing of software testing
做生意更加务实
蒹葭苍苍,白露为霜。
文件服务设计
Docker deployment MySQL 8
用Steam教育启发学生多元化思维