上海婚恋网站排名,从化一站式网站建设,自助建站系统代理,荥阳seo关于 INI 配置文件与go-ini 1 #xff09;概述
在INI配置文件中可以处理各种数据的配置INI文件是一种简单的文本格式#xff0c;常用于配置软件的各种参数go-ini 是地表 最强大、最方便 和 最流行 的 Go 语言 INI 文件操作库 Github 地址#xff1a;https://github.com/go-…关于 INI 配置文件与go-ini 1 概述
在INI配置文件中可以处理各种数据的配置INI文件是一种简单的文本格式常用于配置软件的各种参数go-ini 是地表 最强大、最方便 和 最流行 的 Go 语言 INI 文件操作库 Github 地址https://github.com/go-ini/ini官方文档: https://ini.unknwon.io/使用示例直达 https://ini.unknwon.io/docs/intro/getting_started
2 使用
新建配置文件 conf/app.ini
app_name xxproject
log_level DEBUG # possible values: DEBUG, INFO, WARNING, ERROR, FATAL
admin_path /admin[mysql]
ip 192.168.0.6
port 3306
user root
password 123456
database gin[redis]
ip 127.0.0.1
port 6379在 main.go 文件中测试核心代码
package mainimport (gopkg.in/ini.v1github.com/gin-gonic/gin
)func main() {// 创建一个默认的路由引擎r : gin.Default()//演示 gopkg.in/ini.v1模块的使用config, err : ini.Load(./conf/app.ini)if err ! nil {fmt.Printf(Fail to read file: %v, err)os.Exit(1)}// 获取ini里面的配置fmt.Println(config.Section().Key(app_name).String())fmt.Println(config.Section(mysql).Key(password).String())fmt.Println(config.Section(redis).Key(ip).String())// 给ini写入数据config.Section().Key(app_name).SetValue(yyyproject)config.Section().Key(admin_path).SetValue(/admin)config.SaveTo(./conf/app.ini)r.Run()
}上面演示了获取 ini 数据 和 写入 ini 数据如果是一些比较通用的数据建议写在一个go模块里不用读取ini配置但是关于第三方的一些东西都可以抽离在最外层适用于不同环境下配置不同的值
GIN 不同环境的部署使用不同的配置
在一般的部署中会拆分研发环境测试环境UAT环境和生产环境四种因为一些数据库等第三方配置不可能使用相同的源必定会进行环境隔离这时候就可以设定 development.ini, test.ini, uat.ini, production.ini这里举一个在 Jenkins 中的例子 在Jenkins中设置 ENV 环境变量它的值将用于决定加载哪个INI文件 传递环境变量到Go程序 当Jenkins启动Go程序时确保将ENV环境变量传递给它这样Go程序就可以根据这个变量加载相应的INI文件
下面是 main.go 中的读取示例
package main import (fmtloggopkg.in/ini.v1os
)func loadConfig(env string) (*ini.File, error) {// 根据环境变量或其他机制确定INI文件路径filePath : fmt.Sprintf(./conf/%s.ini, env)cfg, err : ini.Load(filePath)if err ! nil {return nil, err}return cfg, nil
}func main() { // 假设环境变量 ENV 已经设置如 ENVdevelopment env : os.Getenv(ENV) cfg, err : loadConfig(env) if err ! nil { log.Fatalf(Failed to load configuration: %v, err) } // 读取配置信息 dbHost : cfg.Section(database).Key(host).String() dbPort : cfg.Section(database).Key(port).MustInt() // ... 其他配置 fmt.Printf(Database Host: %s, Port: %d\n, dbHost, dbPort)
}这样Jenkins 在不同环境执行部署的时候由于不同环境下的 ENV 配置会设置到当前的系统环境中go程序在读取到ENV的时候会引导读取不同的配置文件由此对各个部署环境进行区分和隔离 文章转载自: http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.gcysq.cn.gov.cn.gcysq.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn