英文:
how to escape ! while unmarshalling the yamlfile in golang
问题
我有一个包含特殊字符!
的键值对的yaml文件,如下所示:
name: Amit Kumar
age: !kk
address:
street: !123 Some Random Street
city: Chennai
state: TN
zip: 763098
phoneNumbers:
- type: home
number: 0123456789
- type: work
number: 0987654321
在解析这个yaml文件时,以!
开头的任何单词都会被移除,例如street: !123 Some Random Street
或age: !dd
。
例如:
person.age
或person.address
将会得到以下结果:
age: kk
address: map[city:Chennai state:TN street:Some Random Street zip:763098]
期望的结果是:
age: !kk
address: map[city:Chennai state:TN street:!123 Some Random Street zip:763098]
有关在解析yaml文件时保留特殊字符的建议吗?
Go文件:
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
)
type Address struct {
Street string `yaml:"street"`
City string `yaml:"city"`
State string `yaml:"state"`
Zip string `yaml:"zip"`
}
type PhoneNumber struct {
Type string `yaml:"type"`
Number string `yaml:"number"`
}
type Person struct {
Name string `yaml:"name"`
Age string `yaml:"age"`
Address interface{} `yaml:"address"`
PhoneNumber []PhoneNumber `yaml:"phoneNumbers"`
}
func main() {
// 读取文件
data, err := ioutil.ReadFile("testyamltemplate.yaml")
if err != nil {
fmt.Println(err)
return
}
// 创建一个结构体来保存YAML数据
var person Person
// 将YAML数据解析到结构体中
err = yaml.Unmarshal(data, &person)
if err != nil {
fmt.Println(err)
return
}
// 打印数据
fmt.Println(person.Age)
}
以上是你要翻译的内容。
英文:
I have yaml file which contain special character !
in the key-value pair as shown below:
name: Amit Kumar
age: !kk
address:
street: !123 Some Random Street
city: Chennai
state: TN
zip: 763098
phoneNumbers:
- type: home
number: 0123456789
- type: work
number: 0987654321
While unmarshall this yaml file, street: !123 Some Random Street
or age: !dd
, any word that begin with !
are removed.
For example:
person.age
or person.address
will result in
age: kk
address: map[city:Chennai state:TN street:Some Random Street zip:763098]
expected:
age: !kk
address: map[city:Chennai state:TN street:!123 Some Random Street zip:763098]
Any suggestion on retaining the special character while unmarshalling the yaml file?
Go file:
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
)
type Address struct {
Street string `yaml:"street"`
City string `yaml:"city"`
State string `yaml:"state"`
Zip string `yaml:"zip"`
}
type PhoneNumber struct {
Type string `yaml:"type"`
Number string `yaml:"number"`
}
type Person struct {
Name string `yaml:"name"`
Age string `yaml:"age"`
Address interface{} `yaml:"address"`
PhoneNumber []PhoneNumber `yaml:"phoneNumbers"`
}
func main() {
// Read the file
data, err := ioutil.ReadFile("testyamltemplate.yaml")
if err != nil {
fmt.Println(err)
return
}
// Create a struct to hold the YAML data
var person Person
// Unmarshal the YAML data into the struct
err = yaml.Unmarshal(data, &person)
if err != nil {
fmt.Println(err)
return
}
// Print the data
fmt.Println(person.Age)
}
答案1
得分: 2
未引用的前导感叹号在YAML规范中被视为标签的开始。
为了防止这种解释,可以用双引号或单引号将整个值包裹起来:
age: "!kk"
street: "!123 Some Random Street"
英文:
Unquoted leading exclamation mark is treated by YAML specification as a start of a tag.
For prevent such interpretation wrap the whole value with double or single quotes:
age: "!kk"
street: "!123 Some Random Street"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论