在Golang中读取YAML文件,而YAML文件包含unicode字符(如"a\u0000b")

kua*_*bin 1 string unicode yaml go

我在读取YAML文件时遇到问题,YAML文件包含Unicode字符转义.但是当我加载YAML文件并打印时fileInfo,包含Unicode字符转义(例如'a\u0000b')的字符串在使用unMarshal()函数时被转义.

这是我的YAML文件(conf.yml):

topicList:
  - source: 'test'
    target: 'temp'
  - source: 'a\u0000b'
    target: 'temp'
Run Code Online (Sandbox Code Playgroud)

我的代码是:

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 
}
Run Code Online (Sandbox Code Playgroud)

结果是:

+++++ sourceTopic =  test

+++++ sourceTopic =  a\u0000b
Run Code Online (Sandbox Code Playgroud)

但我预期的结果是:

+++++ sourceTopic =  test

+++++ sourceTopic =  ab

topic contains unicode character. topic =  ab
Run Code Online (Sandbox Code Playgroud)

为什么我不能得到预期的答案?如何修复代码?谢谢!

icz*_*cza 6

代码很好(不需要修复),问题是你在输入YAML中使用了单引号.单引号中的转义序列被解释为-is(即:未解释),因此'a\u0000b'将表示确切的字符串"a\u0000b",或者在Go解释的字符串文字语法中:"a\\u0000b".

相反,源YAML必须使用双引号:"a\u000b"用于解释/解码转义.

引自YAML规范:转义序列:

请注意,转义序列仅在双引号标量中进行解释.在所有其他标量样式中,"\"字符没有特殊含义,并且不可打印不可用的字符.

如果您将输入YAML更改为:

topicList:
  - source: 'test'
    target: 'temp'
  - source: "a\u0000b"
    target: 'temp'
Run Code Online (Sandbox Code Playgroud)

然后你的应用程序的输出将是:

 +++++ sourceTopic =  test

 +++++ sourceTopic =  ab

 topic contains unicode character. topic =  ab
Run Code Online (Sandbox Code Playgroud)