go - 在 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
解决方案
推荐阅读
- json - Swagger 定义解析为简化的 JS 对象(复杂对象操作)
- django - Django:计算平均食谱评分
- c# - 通过 asp.net core 设置本地存储
- php - Laravel 在控制器构造函数中从数据库中获取应用程序常量
- docker - 我在哪里可以找到 github 操作与预安装软件一起使用的 docker 映像
- c++ - 为什么`std::ranges::clamp`如此严格地限制投影的数量?
- javascript - HttpClient 使用订阅但不使用管道角度
- python - 使用 RDP 时如何使 pyautogui 工作?
- neovim - JSON 文件缺少引号 (")
- node.js - Node JS / Firebase Admin SDK / Firestore - 使用快照接收 Firebase 文档后获取空数组