阅读toml文件时空的结果

发布于 2025-02-07 08:18:26 字数 1055 浏览 1 评论 0原文

我正在尝试阅读go的toml文件。我想拥有不同的文件系统,不仅filesystem.file,例如filesystem.s3,它们定义了不同的路径。但是它仅返回一个空结构{map [file:{map []]}。我想念什么?

我正在使用此库来读取toml文件: https://github.com/burntsushi/burntsushi/toml

TOML文件:

[filesystem.file]
    [filesystem.file.test]
        folder = "tmp/testdata"
    [filesystem.file.test2]
        folder = "tmp/testdata2"
[filesystem.s3]
    [filesystem.s3.test]
        folder = "s3folder/testdata"

我的GO代码:

package main

type 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)
}

I'm trying to read a toml file with Go. I want to have different filesystems not only filesystem.file but for example also filesystem.s3, which have different paths defined. But it only returns an empty struct {map[file:{map[]}]}. What am I missing?

I'm using this library for reading the toml file: https://github.com/BurntSushi/toml

toml file:

[filesystem.file]
    [filesystem.file.test]
        folder = "tmp/testdata"
    [filesystem.file.test2]
        folder = "tmp/testdata2"
[filesystem.s3]
    [filesystem.s3.test]
        folder = "s3folder/testdata"

My go code:

package main

type 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)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2025-02-14 08:18:26

输入中定义的TOML对应于顶级filesystem struct,包含多种类型IE 文件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/play/play/pplay/p/lffkvl4_1zx

The TOML defined in the input corresponds to a top level filesystem struct, containing multiple types i.e. file, s3 etc. So the right way to define the equivalent Go structs to decode those would be to do

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文