Как упорядочить многострочную строку до значения yaml - PullRequest
0 голосов
/ 04 июля 2018

У меня есть структура

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)

и записать сгенерированное содержимое yaml в файл, используя os.file.Write() Но в сгенерированном выше файле 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 следующим образом?

  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 голосов
/ 04 июля 2018

Работает нормально. Ваша многострочная строка не заканчивается новой строкой, поэтому для YAML необходимо убрать новую строку при ее разборе. Именно это и делает |-.

Если вы действительно хотите вместо этого |, просто добавьте новую строку в конец вашей входной строки, но помните о функциональной разнице.

См. этот ответ для полного объяснения различных способов включения многострочных строк в YAML.

...