猿问

为 TOML 文件和 golang 解析表中的键值对

我有以下 TOML 文件结构:


[database]

host = "localhost"

port = 8086

https = true

username = "root"

password = "root"

db = "test"


[cloud]

deviceType = "2be386e9bbae"

deviceId = "119a705fa3b1"

password = "test"

token = "dqpx5vNLLTR34"

endpoint = "mqtts://mqtt1.endpoint.com"


[gps]

#measurement = "gps"

  [gps.msgpack]

  topic = "/evt/gps/msgpack"


  [gps.json]

  topic = "/evt/gps/json"


[imu]

#measurement = "imu"

  [imu.1]

    tag = "NODE1"

    topic = "/evt/imu1/msgpack"

  [imu.2]

    tag = "NODE2"

    topic = "/evt/imu2/msgpack"

我只想在表和表中设置一次measurement键,而不是在and和 for and中冗余设置gpsimumsgpackjson12


使用注释掉的键以下代码有效


心有法竹
浏览 100回答 1
1回答

holdtom

你说:我想在 gps 表中设置测量键并且只可变一次而不是在 msgpack 和 json 以及 1 和 2 中冗余你不这样做是因为 TOML 格式的创建者说:因为我们需要一种体面的人类可读格式,它可以明确地映射到哈希表,而 YAML 规范长达 80 页,这让我很生气。不,JSON 不算数。你知道为什么。如果您需要对一个键具有相同的值,例如,measurement您必须在每个子表中指定您想要的您正确的 TOML 文件:[database]host = "localhost"port = 8086https = trueusername = "root"password = "root"db = "test"[cloud]deviceType = "2be386e9bbae"deviceId = "119a705fa3b1"password = "test"token = "dqpx5vNLLTR34"endpoint = "mqtts://mqtt1.endpoint.com"[gps][gps.msgpack]topic = "/evt/gps/msgpack"measurement = "gps"[gps.json]topic = "/evt/gps/json"measurement = "gps"[imu][imu.1]measurement = "imu"tag = "NODE1"topic = "/evt/imu1/msgpack"[imu.2]measurement = "imu"tag = "NODE2"topic = "/evt/imu2/msgpack"
随时随地看视频慕课网APP

相关分类

Go
我要回答