使用"http://gopkg.in/yaml.v2"提供的yaml.MapItem类型可以保持map有序
使用方法:
import (
"gopkg.in/yaml.v2"
)
type TCPYaml struct {
ESURLs []string `yaml:"es_urls"`
Probers []yaml.MapItem `yaml:"probers"`
}
var tcpYaml TCPYaml
tcpYaml.ESURLs = esURLs
tcpYaml.Probers = make([]yaml.MapItem, 0)
tcpYaml.Probers = append(tcpYaml.Probers, yaml.MapItem{Key: totName, Value: proberItem})
原理:
// MapSlice encodes and decodes as a YAML map.
// The order of keys is preserved when encoding and decoding.
type MapSlice []MapItem
// MapItem is an item in a MapSlice.
type MapItem struct {
Key, Value interface{}
}