title: Go: How Do I Preserve Root Indentation in yaml?
body: <p>I'm having trouble while re-encoding a YAML file with go and <code>yaml.v3</code>.
  Basically I have a YAML file for which the root indentation is 6 (the root is indented by 6 spaces).</p>
  <p>I need to read this file then manipulate a few values and rewrite the file, however when rewriting the YAML structure to file I loose this indentation.
  Any idea of how I can achieve this? otherwise I'll probably re-read the file as a text file and add the indentation.</p>
  <p>Below is the code I'm using.</p>
  <p><strong>YAML file excerpt:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>      doc:
          a: 'default'
          b: 42
          c: 3
          structure: 'flat'
  
       
        use_timezone: ''
        kafka_nodes: 3
        
  </code></pre>
  <p><strong>Parsing the YAML file and writing back to file</strong></p>
  <pre class="lang-golang prettyprint-override"><code>var ymlConfig yaml.Node
  err = yaml.Unmarshal([]byte(pullConfig()), &amp;ymlConfig)
  //code ommited for brievity (some value verification and modification)
  
  file, err := os.OpenFile(&quot;config.yml&quot;, os.O_WRONLY, os.ModeAppend)
  if err != nil {
      panic(err)
  }
  defer file.Close()
  
  d := yaml.NewEncoder(file)
  d.SetIndent(4)// tried changing the indent but it does not change the root
  defer d.Close()
  
  
  if err := d.Encode(&amp;ymlConfig); err != nil {
      panic(err)
  }
  
  </code></pre>
  <p><strong>Result of re encoding</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>doc:
    a: 'default'
    b: 42
    c: 3
    structure: 'flat'
  
       
  use_timezone: ''
  kafka_nodes: 3
        
  </code></pre>