我从头到尾实现了一个Golang的依赖注入框架,并且集成了gin、xorm、redis、cron、消息中间件等功能;自己觉得还挺好用的,推荐给你!也欢迎一起维护!
github地址:https://github.com/gone-io/gone
文档地址:https://goner.fun/
如果可能,请帮忙在github上点个 ⭐️ ;万分感谢!!
文章目录
在gone中提供了通过内置Goners读取配置文件的方法,配置文件格式暂时只支持
.properties
。
举个例子
例子的代码源代码可以在这里找到。
1. 创建mod
go mod init use-config
2. 添加配置文件
mkdir config
touch config/default.properties
config/default.properties 文件内容如下:
my.conf.int=10
my.conf.int8=130
my.conf.float64=10.222
my.conf.string=config test
my.conf.bool=true
my.conf.duration=10h
my.conf.sub.x=100x
my.conf.sub.y=200y
my.conf.subs[0].x=0000x
my.conf.subs[0].y=0000y
my.conf.subs[1].x=1111x
my.conf.subs[1].y=1111y
3. 添加代码
touch main.go
main.go 文件内容如下:
package main
import (
"fmt"
"github.com/gone-io/gone"
"github.com/gone-io/gone/goner/config"
"time"
)
type SubConf struct {
X string `properties:"x"`
Y string `properties:"y"`
}
type UseConfig struct {
gone.Flag
int int `gone:"config,my.conf.int"`
int8 int8 `gone:"config,my.conf.int8"`
printInt *int `gone:"config,my.conf.int8"` //指针 指向int
float64 float64 `gone:"config,my.conf.floa