首页 > 解决方案 > 在 Go With Profile 部分中解组 TOML

问题描述

我目前正在尝试解组配置文件(与 AWS 在使用其 CLI 时使用的配置文件非常相似)。但是,我创建的结构中没有添加任何内容。

这是我的源代码:

func main() {
    config, _ := GetConfig(".")
    fmt.Println(config) <--- Prints "{map[]}"

}

type Configuration struct {
    Id     int64 `toml:"id"`
    Key    int64 `toml:"key"`
}

type Configurations struct {
    Config map[string]Configuration
}

func GetConfig(path string) (configurations Configurations, err error) {
    viper.AddConfigPath(path)
    viper.SetConfigName("config")
    viper.SetConfigType("toml")

    err = viper.ReadInConfig()
    if err != nil {
        fmt.Println("No config file found!")
        return configurations, err
    }

    err = viper.Unmarshal(&configurations)
    if err != nil {
        fmt.Println("No config user found!")
    }
    return configurations, err
}

这是我的config.toml文件中的内容:

[profile]
id  = 123 
key = 456

标签: gounmarshallingtomlviper-go

解决方案


推荐阅读