4

I have a struct

type Products struct {
    Name    string
    Version string
    Description     string
}

holding a multiline string constant as the value for Description field, the constant is as follows

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`

Then I use the package gopkg.in/yaml.v2 for marshaling the above struct (which contains the above constant as a value for its field) as follows

data, err = yaml.Marshal(updateDescriptorV3)

and write the generated yaml content to a file using os.file.Write() But in the yaml file generated above an additional - exists after literal_block symbol (|) What I am doing wrong?

  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

What needs to be done to get the above constant as a literal_block in yaml as follows?

  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
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
  • So `updateDescriptorV3` is an instance of `Products`? – Jonathan Hall Jul 04 '18 at 10:00
  • 2
    @KasunSiyambalapitiya Check [this answer](https://stackoverflow.com/a/21699210/134204). YAML is the opposite of simple. There are 9 ways to write multi-line strings. From that answer `|-: "strip": remove the line feed, remove the trailing blank lines.`. – Panagiotis Kanavos Jul 04 '18 at 10:01
  • Before anyone starts downvoting, do *you* remember the entire YAML spec? Really? It's larger than XML, with different libraries using different rules to serialize. [YAML: Probably not so great after all](https://arp242.net/weblog/yaml_probably_not_so_great_after_all.html). – Panagiotis Kanavos Jul 04 '18 at 10:07
  • 1
    @KasunSiyambalapitiya it's perfectly OK to be surprised by YAML, even experts can't remember every single cryptic combination. In fact, the author of the linked answer had to edit the answer 3 times at least just for multiline strings `There are 5 6 NINE (or 63*, depending how you count) different ways`. The answer has 35 revisions – Panagiotis Kanavos Jul 04 '18 at 10:11
  • You might also want to read my article on YAML quoting: http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html – tinita Jul 04 '18 at 10:26

1 Answers1

2

It is working properly. Your multi-line string does not end in a newline, so it is necessary for the YAML to strip the newline when it is parsed. That is precisely what the |- does.

If you really want | instead, just add a newline to the end of your input string, but be aware of the functional difference.

See this answer for a complete explanation of the various ways to include multi-line strings in YAML.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    That answer is actually used as an [example of YAML's complexity](https://arp242.net/weblog/yaml_probably_not_so_great_after_all.html): `For example did you know there are nine ways to write a multi-line string in YAML with subtly different behaviour? Yeah :-/` – Panagiotis Kanavos Jul 04 '18 at 10:09
  • @PanagiotisKanavos You won't find me defending YAML. I don't need to be convinced it's over complex. – Jonathan Hall Jul 04 '18 at 10:09
  • just a coincidence - the article resurfaced yesterday in twitter discussions about config formats. I just realized it linked to *that* answer as an example of its complexity – Panagiotis Kanavos Jul 04 '18 at 10:15
  • I found this discussion useful. I was trying to figure out why my yaml output didn't split onto multiple lines in some cases. Turns out I needed to remove trailing spaces on some lines that some tools had left in their output. Easy with a rexexp ReplaceAllLiteralString - but it does require some figuring out from time to time. – WeakPointer Mar 01 '22 at 22:06