您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

(继续)如何使用toml文件?

(继续)如何使用toml文件?

首先获取BurntSushi的toml解析器:

go get github.com/BurntSushi/toml

BurntSushi解析toml并将其映射到结构,这就是您想要的。

后执行以下示例并从中学习:

package main

import (
    "github.com/BurntSushi/toml"
    "log"
)

var tomlData = `title = "config"
[feature1]
enable = true
userids = [
  "12345", "67890"
]

[feature2]
enable = false`

type feature1 struct {
    Enable  bool
    Userids []string
}

type feature2 struct {
    Enable bool
}

type tomlConfig struct {
    Title string
    F1    feature1 `toml:"feature1"`
    F2    feature2 `toml:"feature2"`
}

func main() {
    var conf tomlConfig
    if _, err := toml.Decode(tomlData, &conf); err != nil {
        log.Fatal(err)
    }
    log.Printf("title: %s", conf.Title)
    log.Printf("Feature 1: %#v", conf.F1)
    log.Printf("Feature 2: %#v", conf.F2)
}

请注意tomlData和及其如何映射到该tomlConfig结构。

https://github.com/BurntSushi/toml中查看更多示例

其他 2022/1/1 18:19:35 有372人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶