英文:
How to remove apostrophe/byte marks from string sentences when trying to parse yaml
问题
示例代码:
import (
"fmt"
"log"
yaml "gopkg.in/yaml.v3"
)
type X struct {
Example string `yaml:"some-example"`
}
func main() {
item := X{
Example: fmt.Sprint("\"some text\""),
}
res, err := yaml.Marshal(item)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(res))
}
打印结果为 some-example: ''"some text"''
想要的结果是 some-example: "some text"
英文:
Live example here
It seems like the yaml parsing library is unable to print "
So when i parse a sentence with "
, go adds byte apostrophe around it ('
)
Is there some trick to just have it print/make simple string/quotation string, without the byte apostrophes added?
Example code:
import (
"fmt"
"log"
yaml "gopkg.in/yaml.v3"
)
type X struct {
Example string `yaml:"some-example"`
}
func main() {
item := X{
Example: fmt.Sprint("\"some text\""),
}
res, err := yaml.Marshal(item)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(res))
}
Prints some-example: '"some text"'
Want some-example: "some text"
答案1
得分: 1
有没有一些技巧可以让它只打印/生成简单的字符串/引号字符串,而不添加字节的撇号?
请注意,您正在打印yaml.Marshal
的输出,也就是您正在打印一个有效的YAML文档,而YAML没有任何称为"byte apostrophes"的东西。在YAML中,字符串可以是未引用的、双引号引用的或单引号引用的,无论如何,它们都是字符串。
# 以下三个都是字符串
a: foo bar
b: "foo bar"
c: 'foo bar'
所以你的原始输出
some-example: '"some text"'
是完全有效的YAML,而不是Go语言添加了单引号,而是gopkg.in/yaml.v3
包在做这个。
据我所知,不可能为yaml.Encoder
设置一个全局设置,以便将每个字符串都使用双引号样式进行编组,但是您可以使用一个自定义字符串来实现yaml.Marshaler
,以便强制yaml.Encoder
始终为该自定义类型的任何值输出双引号引用的字符串。
例如:
type DoubleQuotedString string
func (s DoubleQuotedString) MarshalYAML() (interface{}, error) {
return yaml.Node{
Kind: yaml.ScalarNode,
Style: yaml.DoubleQuotedStyle, // <- 这是相关的部分
Value: string(s),
}, nil
}
https://go.dev/play/p/o9VNL5mKdSl
some-example: "\"some text\""
英文:
> Is there some trick to just have it print/make simple string/quotation string, without the byte apostrophes added?
Note that you are printing the output of yaml.Marshal
, i.e. you are printing a valid YAML document and YAML does not have anything called "byte apostrophes". In YAML strings can be either unquoted, double quoted, or single quoted, regardless, they are all strings.
# all three are strings
a: foo bar
b: "foo bar"
c: 'foo bar'
So your original output
some-example: '"some text"'
is perfectly valid YAML, and it is not Go that is adding the single quotes, it is the gopkg.in/yaml.v3
package that's doing that.
AFAICT it is not possible to set a global setting for the yaml.Encoder
to marshal every string using the double-quoted style, however you can use a custom string that implements yaml.Marshaler
to force the yaml.Encoder
to always output double quoted strings for any value of that custom type.
For example:
type DoubleQuotedString string
func (s DoubleQuotedString) MarshalYAML() (interface{}, error) {
return yaml.Node{
Kind: yaml.ScalarNode,
Style: yaml.DoubleQuotedStyle, // <- this is the relevant part
Value: string(s),
}, nil
}
https://go.dev/play/p/o9VNL5mKdSl
some-example: "\"some text\""
答案2
得分: 0
相同的问题,使用%+v打印结构体字段时不显示单引号,但在使用gopkg.in/yaml.v3包的编组过程中,单引号被添加了。结果是我的YAML文件(显然是有效的)看起来很奇怪。
k8s_labels:
- ''"nic-type-sriov=false"''
英文:
Same issue, Printing out the field of struct with %+v does not show single quotes but somehow during the marshaling process using the gopkg.in/yaml.v3 package, single quote is being added. As a result my yaml(apparently, even though is valid) looks odd
k8s_labels:
- '"nic-type-sriov=false"'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论