Repository navigation
What is the difference between Get String Map UnMarshall Key and UnMarshall #2000
Replies: 1 comment
|
The difference in your output comes from looking up the parent key versus resolving its individual children. In Viper v1.21.0,
For a database-only result, unmarshal through a small root wrapper: func loadDatabaseConfig() (DatabaseConfig, error) {
var cfg struct {
Database DatabaseConfig `mapstructure:"database"`
}
if err := viper.Unmarshal(&cfg); err != nil {
return DatabaseConfig{}, err
}
return cfg.Database, nil
}Keep your existing |
Uh oh!
There was an error while loading. Please reload this page.
package main
import (
"fmt"
"log"
"os"
"strings"
)
type Config struct {
Root string
mapstructure:"root"App AppConfig
mapstructure:"app"Server ServerConfig
mapstructure:"server"Database DatabaseConfig
mapstructure:"database"}
type AppConfig struct {
Name string
mapstructure:"name"}
type ServerConfig struct {
Port int
mapstructure:"port"}
type DatabaseConfig struct {
Host string
mapstructure:"host"Port int
mapstructure:"port"Name string
mapstructure:"name"MaxOpen int
mapstructure:"max_open"}
func init() {
os.Setenv("MYAPP_ROOT", "Env_Test")
os.Setenv("MYAPP_APP__NAME", "Env_OracleTest")
os.Setenv("MYAPP_SERVER__PORT", "8090")
os.Setenv("MYAPP_DATABASE__HOST", "Env_sulink")
os.Setenv("MYAPP_DATABASE__PORT", "1522")
os.Setenv("MYAPP_DATABASE__NAME", "Env_testclr")
os.Setenv("MYAPP_DATABASE__MAX_OPEN", "30")
}
func bindEnvVars() {
viper.BindEnv("root", "MYAPP_ROOT")
viper.BindEnv("app.name", "MYAPP_APP__NAME")
viper.BindEnv("server.port", "MYAPP_SERVER__PORT")
viper.BindEnv("database.host", "MYAPP_DATABASE__HOST")
viper.BindEnv("database.port", "MYAPP_DATABASE__PORT")
viper.BindEnv("database.name", "MYAPP_DATABASE__NAME")
viper.BindEnv("database.max_open", "MYAPP_DATABASE__MAX_OPEN")
}
func main() {
root := viper.GetString("root")
appName := viper.GetString("app.name")
serverPort := viper.GetInt("server.port")
dbHost := viper.GetString("database.host")
maxOpen := viper.GetInt("database.max_open")
}
Output
Starting: C:\Users\39636\go\bin\dlv.exe dap --listen=127.0.0.1:62021 from d:\Study\Go\oracletest
2025-03-19T18:16:51+08:00 warn layer=dlv CGO_CFLAGS already set, Cgo code could be optimized.
DAP server listening at: 127.0.0.1:62021
Type 'dlv help' for list of commands.
AllSettings:map[app:map[name:Env_OracleTest] database:map[host:Env_sulink max_open:30 name:Env_testclr port:1522] root:Env_Test server:map[port:8090]]
Config loaded: {Root:Env_Test App:{Name:Env_OracleTest} Server:{Port:8090} Database:{Host:Env_sulink Port:1522 Name:Env_testclr MaxOpen:30}}
DatabaseMap: map[host:sulink max_open:10 name:testclr port:1521]
DatabaseConfig loaded: {Host:sulink Port:1521 Name:testclr MaxOpen:10}
Root: Env_Test
Application Name: Env_OracleTest
Server Port: 8090
Database Host: Env_sulink
Database MaxOpen: 30
Process 20056 has exited with status 0
Viper.getStringMap ("database"), viper.UnmarshalKey ("database",&dbConfig) with the value of config. yaml
The value of viper. Unimarshal (&cfg) is the environment variable
My true intention is to prioritize environment variables, regardless of whether this KEY exists in the configuration file or not
All reactions