title: What is the way to set default values on keys in lists when unmarshalling Yaml in Go?
body: <p>I have a YAML like one below.</p>
  
  <pre><code>connections:
    - name: demo
      hosts:
        - demo.example.com:9200
      username: admin
      password: password
      ssl: true
      ssl_verify: true
      version: 7
    - name: test
      hosts:
        - "test.example.com:9200"
      username: admin
      password: password
  </code></pre>
  
  <p>As you can see <code>ssl</code> and <code>ssl_verify</code> is not set in the second item of the list. I want them by default to be <code>true</code>, however, it is not happening. I tried different solutions.</p>
  
  <ol>
  <li>Viper defaults - does not work.</li>
  </ol>
  
  <pre><code>viper.SetDefault("connections[].ssl", "true")
  </code></pre>
  
  <ol start="2">
  <li><a href="https://github.com/creasty/defaults" rel="noreferrer">https://github.com/creasty/defaults</a> - does not work.</li>
  </ol>
  
  <pre><code>type Config struct {
      Connections []struct {
          Name      string
          Hosts     []string
          Username  string
          Password  string
          Ssl       bool `default:"true"`
          SslVerify bool `default:"true"`
          Version   int
      }
  }
  
  ...
  
  err := defaults.Set(config)
  
  </code></pre>
  
  <ol start="3">
  <li><p>Manually looping through the list of structures. While these method work with strings, it does not work with boolean values because they are already initialized with <code>false</code> after unmarshalling and we don't know for sure whether <code>false</code> is entered by the user or not.</p></li>
  <li><p>Using pointers with boolean values. This works as uninitialized values are equal to <code>nil</code> and they are easy to catch. However, it will require to dereference pointers when using config struct, which is not very convenient. Alternatively, a new struct can be generated based on the one that comes from unmarshalling.</p></li>
  </ol>
  
  <pre><code>type Config struct {
      Connections []struct {
          Name      string
          Hosts     []string
          Username  string
          Password  string
          Ssl       *bool
          SslVerify *bool
          Version   int
      }
  }
  </code></pre>
  
  <ol start="5">
  <li>Using hashmap instead of struct. This works because empty values are not initialized, however, it will require to run checks on the map elements before accessing them or to convert a map to the struct.</li>
  </ol>
  
  <p>Solutions 4 and 5 will likely work, but I am wondering if there anything better than that.</p>
  
  <p>Any ideas?</p>