dt_automate/conf/yaml.go
2025-02-27 14:27:45 +08:00

35 lines
784 B
Go

package conf
import (
"fmt"
"github.com/spf13/viper"
)
func init() {
// 设置配置文件的名字
viper.SetConfigName("test")
// 设置配置文件的类型
viper.SetConfigType("yaml")
// 添加配置文件的路径,指定 config 目录下寻找
viper.AddConfigPath("config/")
// 寻找配置文件并读取
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
// 修改配置文件内容
func SET_Config_yaml(name, value string) {
viper.Set(name, value)
// fmt.Println(viper.Get("mysql")) // map[port:3306 url:127.0.0.1]
// fmt.Println(viper.Get("mysql.url")) // 127.0.0.1
}
// 读取配置文件内容
func GET_Config_yaml(name string) any {
return viper.Get(name)
}