package conf import ( "fmt" "github.com/spf13/viper" ) func init() { // 设置配置文件的名字 viper.SetConfigName("test") // 设置配置文件的类型 viper.SetConfigType("yaml") // 添加配置文件的路径,指定 config 目录下寻找 viper.AddConfigPath("conf/") // 寻找配置文件并读取 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) viper.WriteConfig() // 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) }