Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在golang中读取YAML文件,而YAML文件包含unicode字符(如“a\u0000b”)_String_Go_Unicode_Yaml - Fatal编程技术网

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

String 在golang中读取YAML文件,而YAML文件包含unicode字符(如“a\u0000b”),string,go,unicode,yaml,String,Go,Unicode,Yaml,我在读取YAML文件时遇到问题,YAML文件包含Unicode字符转义。但是,当我加载YAML文件并打印fileInfo时,使用unMarshal()函数时,包含Unicode字符转义(例如'a\u0000b')的字符串被转义 这是我的YAML文件(conf.yml): topicList: - source: 'test' target: 'temp' - source: 'a\u0000b' target: 'temp' import ( "fmt"

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

这是我的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
我的代码是:

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
结果是:

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
但我预期的结果是:

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
为什么我不能得到预期的答案?如何修复代码?谢谢大家!

代码很好(无需修复),问题是您在输入YAML中使用了单引号。单引号中的转义序列被解释为原样(即:未解释),因此
'a\u0000b'
将表示确切的字符串
“a\u0000b”
,或Go解释的字符串文字语法:
“a\\u0000b”

相反,源YAML必须使用双引号:
“a\u000b”
才能解释/解码转义

引用

请注意,转义序列仅在中解释。在所有其他情况下,“\”字符没有特殊含义,非字符不可用

如果将输入YAML更改为:

topicList:
  - source: 'test'
    target: 'temp'
  - source: "a\u0000b"
    target: 'temp'
然后,应用程序的输出将为:

 +++++ sourceTopic =  test

 +++++ sourceTopic =  ab

 topic contains unicode character. topic =  ab
代码很好(无需修复),问题是在输入YAML中使用了单引号。单引号中的转义序列被解释为原样(即:未解释),因此
'a\u0000b'
将表示确切的字符串
“a\u0000b”
,或Go解释的字符串文字语法:
“a\\u0000b”

相反,源YAML必须使用双引号:
“a\u000b”
才能解释/解码转义

引用

请注意,转义序列仅在中解释。在所有其他情况下,“\”字符没有特殊含义,非字符不可用

如果将输入YAML更改为:

topicList:
  - source: 'test'
    target: 'temp'
  - source: "a\u0000b"
    target: 'temp'
然后,应用程序的输出将为:

 +++++ sourceTopic =  test

 +++++ sourceTopic =  ab

 topic contains unicode character. topic =  ab

术语说明:所有字符都是unicode字符。您的问题是转义,而不是unicode。为什么在使用unMarshal()函数时转义包含“\u0000”的字符串?如果我fmt.Println(“a\u0000b”),返回“ab”。因为
fmt.Println
解释转义序列,但yaml解组器不解释。好的,如何修复代码?在yaml unmarshal()中找不到solve函数。谢谢。术语说明:所有字符都是unicode字符。您的问题是转义,而不是unicode。为什么在使用unMarshal()函数时转义包含“\u0000”的字符串?如果我fmt.Println(“a\u0000b”),返回“ab”。因为
fmt.Println
解释转义序列,但yaml解组器不解释。好的,如何修复代码?在yaml unmarshal()中找不到solve函数。非常感谢。