What is the difference between Get String Map UnMarshall Key and UnMarshall #2000
Unanswered
agileboy365
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Beta Was this translation helpful? Give feedback.
All reactions