package models import ( "fmt" "log" "path/filepath" "runtime" "github.com/spf13/viper" ) var ( _, b, _, _ = runtime.Caller(0) Root = filepath.Join(filepath.Dir(b), "..") Viper *viper.Viper ) func init() { //如果命令行参数中有test,则使用测试环境的配置 config_file := filepath.Join(Root, "data", "config.yaml") fmt.Println(config_file) viper.SetConfigFile(config_file) if err := viper.ReadInConfig(); err != nil { log.Println("读取配置文件失败", err) 生成配置文件() } Viper = viper.GetViper() } func 生成配置文件() { viper.Set("mysql.host", "") viper.Set("mysql.port", 3306) viper.Set("mysql.user", "") viper.Set("mysql.password", "") viper.Set("mysql.dbname", "") viper.Set("mysql.charset", "utf8mb4") viper.Set("mysql.maxOpenConns", 100) viper.Set("oss.endpoint", "") viper.Set("oss.accessID", "") viper.Set("oss.accessKey", "") viper.Set("video.endpoint", "") viper.Set("video.accessKeyID", "") viper.Set("video.accessKey", "") }