首页 文章

如何将多行字符串编组为yaml值

提问于
浏览
2

我有一个结构

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

1 回答

  • 0

    它运作正常 . 您的多行字符串不以换行符结尾,因此YAML必须在解析时删除换行符 . 这正是 |- 所做的 .

    如果您真的想要 | ,只需在输入字符串的末尾添加换行符,但要注意功能差异 .

    有关在YAML中包含多行字符串的各种方法的完整说明,请参阅this answer .

相关问题