gpt4 book ai didi

go - 带 viper 的动态组名

转载 作者:IT王子 更新时间:2023-10-29 02:20:54 25 4
gpt4 key购买 nike

我正在尝试从配置中获取一些名称未知的组(稍后会获取更多)。

我是 golang 的新手,有点吃力。我正在使用 Viper,因为它支持 yaml、json 和 toml。

json 配置:

{
"database": {
"db": "mydb",
"host": "localhost",
"pass": "mypassword",
"port": 3306,
"user": "username"
},
"customers": {
"company_one": {
"address": "66 Great Queen St, London WC2B 5BX, UK",
"contacts": [
{
"email": "joe.doe@company-one.local",
"name": "Joe Doe"
},
{
"email": "jane.doe@company-one.local",
"name": "Jane Doe"
}
]
},
"company_two": {
"address": "Irish Town Pl, Gibraltar GX11 1AA, Gibraltar",
"contacts": [
{
"email": "lucky.luke@second-company.local",
"name": "Lucky Luke"
}
]
}
}
}

和来源:

package main

import (
"fmt"
"log"
"github.com/spf13/viper"
)

type Config struct {
database Database
customers Customer
}

type Database struct {
host string
port uint16
user string
pass string
db string
}

type Customer struct {
company Company
}

type Company struct {
contacts Contact
address string
}

type Contact struct {
name string
email string
}

func main() {
viper.SetConfigName("a")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("Unable to read config file", err)
}
var conf Config
database := viper.GetStringMap("database")
for key, i := range database {
switch key {
case "host":
conf.database.host = i.(string)
case "port":
conf.database.port = i.(uint16)
case "user":
conf.database.user = i.(string)
case "pass":
conf.database.pass = i.(string)
case "db":
conf.database.db = i.(string)
}
}
fmt.Printf("%v\n", conf)
}

我不知道如何循环浏览客户。

我已经尝试过这个(从 go-toml 转换为 viper),但它没有按预期工作:

var contMap = map[string]Contacts

cust := config.Get("customers")

for _, c := range cust.Keys() {
sub := cust.Get(c).([]interface{})
for _,d := range sub{
address := d.([]interface{})[0].(string)
hostMap[host] = Contacts{
email: email,
name: name,
}
}
log.Printf("Customers: %s contact: %q", c, sub)
}

最佳答案

我开始着手回答您最初的问题,但它太麻烦了,而且可能不是您真正想要的。

您粘贴的代码中有很多错误,所以让我提供一个更简单的解决方案,就像我在评论中所说的那样。

package main

import (
"log"

"github.com/spf13/viper"
)

type Config struct {
Database Database `mapstructure:"database"`
Customers map[string]Company `mapstructure:"customers"`
}

type Database struct {
Host string `mapstructure:"host"`
Port uint16 `mapstructure:"port"`
User string `mapstructure:"user"`
Pass string `mapstructure:"pass"`
Db string `mapstructure:"db"`
}

type Company struct {
Address string `mapstructure:"address"`
Contacts []Contact `mapstructure:"contacts"`
}

type Contact struct {
Name string `mapstructure:"name"`
Email string `mapstructure:"email"`
}

func main() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("Unable to read config file", err)
}
var conf Config
err := viper.Unmarshal(&conf)
if err != nil {
panic(err)
}

log.Printf("%#v", conf)
}

如果您使用 JSON 配置文件(名为 config)运行此代码,它会返回以下输出:

2018/04/28 14:47:54 main.Config{Database:main.Database{Host:"localhost", Port:0xcea, User:"username", Pass:"mypassword", Db:"mydb"}, Customers:map[string]main.Company{"company_two":main.Company{Address:"Irish Town Pl, Gibraltar GX11 1AA, Gibraltar", Contacts:[]main.Contact{main.Contact{Name:"Lucky Luke", Email:"lucky.luke@second-company.local"}}}, "company_one":main.Company{Address:"66 Great Queen St, London WC2B 5BX, UK", Contacts:[]main.Contact{main.Contact{Name:"Joe Doe", Email:"joe.doe@company-one.local"}, main.Contact{Name:"Jane Doe", Email:"jane.doe@company-one.local"}}}}}

如果您在代码中创建整个结构,它会在此处重新格式化:

 Config{
Database{
Host: "localhost",
Port: 0xcea,
User: "username",
Pass: "mypassword",
Db: "mydb"},
Customers: map[string]Company{
"company_two": Company{Address: "Irish Town Pl, Gibraltar GX11 1AA, Gibraltar",
Contacts: []Contact{
Contact{Name: "Lucky Luke", Email: "lucky.luke@second-company.local"}}},
"company_one": Company{Address: "66 Great Queen St, London WC2B 5BX, UK",
Contacts: []Contact{
Contact{Name: "Joe Doe", Email: "joe.doe@company-one.local"},
Contact{Name: "Jane Doe", Email: "jane.doe@company-one.local"}}},
},
}

关于go - 带 viper 的动态组名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com