This commit is contained in:
Yun
2024-04-13 10:43:10 +08:00
parent 9913cfa952
commit 9b497910e1
3 changed files with 27 additions and 2 deletions
+1
View File
@@ -0,0 +1 @@
name: "名字"
+8 -2
View File
@@ -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)
}
+18
View File
@@ -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"`
}