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

问题描述 投票:1回答:1

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

这是我的YAML文件(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

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

string go unicode yaml
1个回答
6
投票

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

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

引自YAML spec: Escape Sequences:

请注意,转义序列仅在double-quoted scalars中解释。在所有其他scalar styles中,“\”字符没有特殊含义,并且没有非printable字符。

如果您将输入YAML更改为:

topicList:
  - source: 'test'
    target: 'temp'
  - source: "a\u0000b"
    target: 'temp'

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

 +++++ sourceTopic =  test

 +++++ sourceTopic =  ab

 topic contains unicode character. topic =  ab
© www.soinside.com 2019 - 2024. All rights reserved.