提交
This commit is contained in:
@@ -3,18 +3,24 @@ package yamlx
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// 初始化配置
|
||||
func InitConfig(path string, onfigData interface{}) {
|
||||
func InitConfig(path string, configData interface{}) {
|
||||
// 判断是否指针
|
||||
if reflect.ValueOf(configData).Kind() != reflect.Ptr {
|
||||
panic("接收数据需要是指针类型")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Panicf("read config file error,%v", err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(data, &onfigData)
|
||||
err = yaml.Unmarshal(data, configData)
|
||||
if err != nil {
|
||||
log.Panicf("parse config file error, %v", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package yamlx_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"code.yun.ink/pkg/yamlx"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
c := Config{}
|
||||
yamlx.InitConfig("./demo.yaml", &c)
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
}
|
||||
Reference in New Issue
Block a user