【发布时间】:2018-12-12 17:23:47
【问题描述】:
我有一个结构
type Products struct {
Name string
Version string
Description string
}
持有一个多行字符串常量作为Description字段的值,常量如下
const DEFAULT_DESCRIPTION = `Please add the description here without removing the literal block (|)
Sample description will be as follows,
Fixes for the following in rabbitmq transport
(i) Channel not closing issue in rabbitmq publisher
(ii) Performance improvements to the transport by introducing a channel pool (configurable) and parameterising queue and exchange creation.
(iii) Allow configuring connection factory by name in target url`
然后我使用包gopkg.in/yaml.v2 来编组上述结构(其中包含上述常量作为其字段的值),如下所示
data, err = yaml.Marshal(updateDescriptorV3)
并使用os.file.Write() 将生成的yaml 内容写入文件
但是在上面生成的yaml 文件中,在literal_block 符号(|)之后存在一个额外的- 我做错了什么?
description: |-
Please add the description here without removing the literal block (|)
Sample description will be as follows,
Fixes for the following in rabbitmq transport
(i) Channel not closing issue in rabbitmq publisher
(ii) Performance improvements to the transport by introducing a channel pool (configurable) and parameterising queue and exchange creation.
(iii) Allow configuring connection factory by name in target url
需要做些什么才能将上述常量作为yaml中的literal_block如下?
description: |
Please add the description here without removing the literal block (|)
Sample description will be as follows,
Fixes for the following in rabbitmq transport
(i) Channel not closing issue in rabbitmq publisher
(ii) Performance improvements to the transport by introducing a channel pool (configurable) and parameterising queue and exchange creation.
(iii) Allow configuring connection factory by name in target url
【问题讨论】:
-
所以
updateDescriptorV3是Products的一个实例? -
@KasunSiyambalapitiya 检查this answer。 YAML 与简单相反。有 9 种写多行字符串的方法。从那个答案
|-: "strip": remove the line feed, remove the trailing blank lines.. -
在有人开始反对之前,你还记得整个 YAML 规范吗?真的吗?它比 XML 大,不同的库使用不同的规则进行序列化。 YAML: Probably not so great after all.
-
@KasunSiyambalapitiya 对 YAML 感到惊讶是完全可以的,即使是专家也无法记住每一个神秘的组合。事实上,链接答案的作者必须至少为多行字符串
There are 5 6 NINE (or 63*, depending how you count) different ways编辑答案 3 次。答案有 35 个修订版 -
您可能还想阅读我关于 YAML 引用的文章:blogs.perl.org/users/tinita/2018/03/…
标签: parsing go yaml multilinestring