第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 Golang 中將 Toml 格式轉(zhuǎn)換為 Json 格式

在 Golang 中將 Toml 格式轉(zhuǎn)換為 Json 格式

Go
千萬里不及你 2022-10-24 17:04:58
我有 toml 文件,需要轉(zhuǎn)換為 Json,反之亦然,在 golang 包中它只有命令工具而不是函數(shù)。如果有人對(duì)如何在 Go 語言中將 toml 轉(zhuǎn)換為 json 和反之亦然有準(zhǔn)確的想法,那就太好了
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊

我會(huì)使用一些流行的 toml 庫將 toml 解析為結(jié)構(gòu),然后使用標(biāo)準(zhǔn)庫將其編組為 JSON。


下面的代碼使用了https://github.com/BurntSushi/toml。


請注意,為簡潔起見,沒有錯(cuò)誤處理。


type Config struct {

    Age        int       `json:"age,omitempty"`

    Cats       []string  `json:"cats,omitempty"`

    Pi         float64   `json:"pi,omitempty"`

    Perfection []int     `json:"perfection,omitempty"`

    DOB        time.Time `json:"dob,omitempty"`

}


var tomlData = `

Age = 25

Cats = [ "Cauchy", "Plato" ]

Pi = 3.14

Perfection = [ 6, 28, 496, 8128 ]

DOB = 1987-07-05T05:45:00Z`


func main() {

    var conf Config

    toml.Decode(tomlData, &conf)

    j, _ := json.MarshalIndent(conf, "", "  ")

    fmt.Println(string(j))

}

輸出:


{

  "age": 25,

  "cats": [

    "Cauchy",

    "Plato"

  ],

  "pi": 3.14,

  "perfection": [

    6,

    28,

    496,

    8128

  ],

  "dob": "1987-07-05T05:45:00Z"

}

https://play.golang.com/p/91JqFjkJIXI


查看完整回答
反對(duì) 回復(fù) 2022-10-24
  • 1 回答
  • 0 關(guān)注
  • 356 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)