# 安装 viper
go get github.com/spf13/viper |
# 读取配置
# 直接读取
# config.yaml | |
app_name: My Go App | |
app_port: 8080 | |
debug_mode: true |
package main | |
import ( | |
"fmt" | |
"github.com/spf13/viper" | |
) | |
func main() { | |
// 设置配置文件的名称和路径(可选) | |
viper.SetConfigName("config") | |
viper.SetConfigType("yaml") | |
viper.AddConfigPath(".") | |
// 读取配置文件 | |
err := viper.ReadInConfig() | |
if err != nil { | |
panic(fmt.Errorf("Failed to read config file: %s", err)) | |
} | |
// 读取配置项 | |
appName := viper.GetString("app_name") | |
appPort := viper.GetInt("app_port") | |
debugMode := viper.GetBool("debug_mode") | |
// 使用读取到的配置项 | |
fmt.Println("App Name:", appName) | |
fmt.Println("App Port:", appPort) | |
fmt.Println("Debug Mode:", debugMode) | |
} | |
/* | |
App Name: My Go App | |
App Port: 8080 | |
Debug Mode: true | |
*/ |
# 结构体映射
# config.yaml | |
app: | |
name: My Go App | |
port: 8080 | |
debug_mode: true | |
database: | |
host: localhost | |
port: 3306 | |
username: root | |
password: password | |
dbname: mydb |
package main | |
import ( | |
"fmt" | |
"github.com/spf13/viper" | |
) | |
type Config struct { | |
App AppConfig `mapstructure:"app"` | |
Database DatabaseConfig `mapstructure:"database"` | |
} | |
type AppConfig struct { | |
Name string `mapstructure:"name"` | |
Port int `mapstructure:"port"` | |
DebugMode bool `mapstructure:"debug_mode"` | |
} | |
type DatabaseConfig struct { | |
Host string `mapstructure:"host"` | |
Port int `mapstructure:"port"` | |
Username string `mapstructure:"username"` | |
Password string `mapstructure:"password"` | |
DBName string `mapstructure:"dbname"` | |
} | |
func main() { | |
// 设置配置文件的名称和路径(可选) | |
viper.SetConfigName("config") | |
viper.SetConfigType("yaml") | |
viper.AddConfigPath(".") | |
// 读取配置文件 | |
err := viper.ReadInConfig() | |
if err != nil { | |
panic(fmt.Errorf("failed to read config file: %s", err)) | |
} | |
// 解析配置文件到结构体 | |
var config Config | |
err = viper.Unmarshal(&config) | |
if err != nil { | |
panic(fmt.Errorf("Failed to unmarshal config: %s", err)) | |
} | |
// 使用解析后的结构体 | |
fmt.Println("App Name:", config.App.Name) | |
fmt.Println("App Port:", config.App.Port) | |
fmt.Println("Debug Mode:", config.App.DebugMode) | |
fmt.Println("Database Host:", config.Database.Host) | |
fmt.Println("Database Port:", config.Database.Port) | |
fmt.Println("Database Username:", config.Database.Username) | |
fmt.Println("Database Password:", config.Database.Password) | |
fmt.Println("Database Name:", config.Database.DBName) | |
} | |
/* | |
App Name: My Go App | |
App Port: 8080 | |
Debug Mode: true | |
Database Host: localhost | |
Database Port: 3306 | |
Database Username: root | |
Database Password: password | |
Database Name: mydb | |
*/ |
# 配置中心读取 content
package main | |
import ( | |
"fmt" | |
"github.com/spf13/viper" | |
) | |
type Config struct { | |
App AppConfig `mapstructure:"app"` | |
Database DatabaseConfig `mapstructure:"database"` | |
} | |
type AppConfig struct { | |
Name string `mapstructure:"name"` | |
Port int `mapstructure:"port"` | |
DebugMode bool `mapstructure:"debug_mode"` | |
} | |
type DatabaseConfig struct { | |
Host string `mapstructure:"host"` | |
Port int `mapstructure:"port"` | |
Username string `mapstructure:"username"` | |
Password string `mapstructure:"password"` | |
DBName string `mapstructure:"dbname"` | |
} | |
func main() { | |
// 假设已经从配置中心获取了配置文件内容,并将其保存在 content 变量中 | |
content := ` | |
app: | |
name: My Go App | |
port: 8080 | |
debug_mode: true | |
database: | |
host: localhost | |
port: 3306 | |
username: root | |
password: password | |
dbname: mydb | |
` | |
// 使用 viper.ReadConfigFromReader 来解析配置文件字符串 | |
viper.SetConfigType("yaml") | |
err := viper.ReadConfig(strings.NewReader(content)) | |
if err != nil { | |
panic(fmt.Errorf("Failed to read config: %s", err)) | |
} | |
// 解析配置文件到结构体 | |
var config Config | |
err = viper.Unmarshal(&config) | |
if err != nil { | |
panic(fmt.Errorf("Failed to unmarshal config: %s", err)) | |
} | |
// 使用解析后的结构体 | |
fmt.Println("App Name:", config.App.Name) | |
fmt.Println("App Port:", config.App.Port) | |
fmt.Println("Debug Mode:", config.App.DebugMode) | |
fmt.Println("Database Host:", config.Database.Host) | |
fmt.Println("Database Port:", config.Database.Port) | |
fmt.Println("Database Username:", config.Database.Username) | |
fmt.Println("Database Password:", config.Database.Password) | |
fmt.Println("Database Name:", config.Database.DBName) | |
} | |
/* | |
App Name: My Go App | |
App Port: 8080 | |
Debug Mode: true | |
Database Host: localhost | |
Database Port: 3306 | |
Database Username: root | |
Database Password: password | |
Database Name: mydb | |
*/ |
# 多环境支持
package main | |
import ( | |
"fmt" | |
"github.com/spf13/viper" | |
"os" | |
) | |
func main() { | |
var value string | |
fmt.Scan(&value) | |
// 设置环境变量 | |
os.Setenv("env", value) | |
// 获取设置的环境变量的值 | |
env := os.Getenv("env") | |
// 设置配置文件的名称和路径(可选) | |
viper.SetConfigName("config") | |
viper.SetConfigType("yaml") | |
viper.AddConfigPath(".") | |
// 根据不同的环境加载不同的配置文件 | |
viper.SetConfigName("config_" + env) | |
// 读取配置文件 | |
err := viper.ReadInConfig() | |
if err != nil { | |
panic(fmt.Errorf("failed to read config file: %s", err)) | |
} | |
// 读取配置项 | |
appName := viper.GetString("app.name") | |
appPort := viper.GetInt("app.port") | |
debugMode := viper.GetBool("app.debug_mode") | |
// 使用读取到的配置项 | |
fmt.Println("App Name:", appName) | |
fmt.Println("App Port:", appPort) | |
fmt.Println("Debug Mode:", debugMode) | |
} |