fileInfo'a\u0000b'unMarshal()
conf.yml
topicList:
- source: 'test'
target: 'temp'
- source: 'a\u0000b'
target: 'temp'
我的代码是:
import (
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
TopicList []Topic `yaml:"topicList"`
}
type Topic struct {
Source string `yaml:"source" json:"source"`
Target string `yaml:"target" json:"target"`
}
func main() {
cfg, err := NewConfig("conf.yml")
if err != nil {
fmt.Println("load config fail: ", err)
}
for _, s := range cfg.TopicList {
fmt.Println("\n +++++ sourceTopic = ", s.Source)
if strings.Contains(s.Source, "\u0000") {
fmt.Println("\n topic contains unicode character. topic = ", s.Source)
}
}
}
func NewConfig(file string) (conf *Config, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return
}
cOnf= &Config{}
err = yaml.Unmarshal(data, conf)
return
}
结果是:
+++++ sourceTopic = test +++++ sourceTopic = a\u0000b
但我预期的结果是:
+++++ sourceTopic = test +++++ sourceTopic = ab topic contains unicode character. topic = ab
为什么我不能得到预期的答案?如何修复代码?谢谢!
1> icza..:
'a\u0000b'"a\u0000b""a\\u0000b"
"a\u000b"
引自YAML规范:转义序列:
请注意,转义序列仅在双引号标量中进行解释.在所有其他标量样式中,"\"字符没有特殊含义,并且不可打印不可用的字符.
如果您将输入YAML更改为:
topicList:
- source: 'test'
target: 'temp'
- source: "a\u0000b"
target: 'temp'
然后你的应用程序的输出将是:
+++++ sourceTopic = test +++++ sourceTopic = ab topic contains unicode character. topic = ab