为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Go 读取 TOML 文件时结果为空

使用 Go 读取 TOML 文件时结果为空

Go
慕码人2483693 2022-12-19 21:21:18
我正在尝试用 Go 读取一个 toml 文件。我不仅希望拥有不同的文件系统,filesystem.file而且还希望拥有不同的文件系统filesystem.s3,它们定义了不同的路径。但它只返回一个空的 struct {map[file:{map[]}]}。我错过了什么?我正在使用这个库来读取 toml 文件:https ://github.com/BurntSushi/toml文件:[filesystem.file]    [filesystem.file.test]        folder = "tmp/testdata"    [filesystem.file.test2]        folder = "tmp/testdata2"[filesystem.s3]    [filesystem.s3.test]        folder = "s3folder/testdata"我的代码:package maintype File struct {    Folder string `toml:"folder"`}type FileSystem struct {    File map[string]File `toml:"file"`}type Config struct {    FileSystem  map[string]FileSystem `toml:"filesystem"`}func main() {    var conf Config    _, err := toml.DecodeFile("test.toml", &conf)    if err != nil {        log.Fatalln("Error on loading config: ", err)    }    log.Printf("config: %v", conf)}
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

输入中定义的 TOML 对应于一个顶级filesystem结构,包含多种类型 iefile等s3。因此定义等效的 Go 结构来解码这些结构的正确方法是


type File struct {

    Folder string `toml:"folder"`

}


type FileSystem struct {

    File map[string]File `toml:"file"`

    S3   map[string]File `toml:"s3"`

}


type Config struct {

    FileSystem FileSystem `toml:"filesystem"`

}

https://go.dev/play/p/lfFKVL4_1zx


查看完整回答
反对 回复 2022-12-19
  • 1 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信