亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號