golang encode multiple yaml struct into single file with '---' separator

Viewed 28

I have multiple structs and want to convert them to yaml and put them in a single file with a '---' separator between yamls.

fieldA: valueA
---
fieldB: valueB

fieldA and fieldB are part of two different structs.

I used the gopkg.in/yaml.v2 package and encoding to generate yaml. Now a/c to Encode method documentation of yaml.v2 package

// Encode writes the YAML encoding of v to the stream.
// If multiple items are encoded to the stream, the
// second and subsequent document will be preceded
// with a "---" document separator, but the first will not.

Based on the above, I tried something like this:

    var config bytes.Buffer
    encoder := yaml.NewEncoder(&config)

    encoder.Encode(&structA)
    encoder.Encode(&structB)

    encoder.Close()

but it didn't add the separator in between. However, I can always add encoder.Encode("---") between the two Encode methods.

But is there a better way to achieve this yaml?

0 Answers
Related