Erlo

golang toml解析

2021-04-18 22:01:41 发布   256 浏览  
页面报错/反馈
收藏 点赞

TOML 的全称是 Tom’s Obvious, Minimal Language,因为它的作者是 GitHub 联合创始人 Tom Preston-Werner。

TOML 的目标是成为一个极简的配置文件格式。TOML 被设计成可以无歧义地被映射为哈希表,从而被多种语言解析。

github: https://github.com/BurntSushi/toml
安装:go get github.com/BurntSushi/toml

package main

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

type songInfo struct {
    Name     string
    Duration int
}

type config struct {
    Bc string
    Song songInfo
}

func test_toml() {
    var cg config
    var cpath string = "./example.toml"
    if _, err := toml.DecodeFile(cpath, &cg); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%v %vn", cg.Bc, cg.Song.Name)
}

func main() {
    test_toml()
}

/*
result:
robert-homedeMacBook-Pro:toml robert-home$ go run test_toml.go
坐标北京 北京.北京
 */

example.toml

Bc = “坐标北京”
[song]
Name = “北京.北京”
Duration = 900

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认