title: How to test for default values in Golang when yaml has already initialized the value?
body: <p>I'm writing a daemon's configuration handler and utilizing the <code>yaml</code> package to do so.  Importing my file works like this:</p>
  
  <pre><code>package daemon
  
  import (
      "ioutil"
      "log"
  
      "gopkg.in/yaml.v2"
  )
  
  type daemonConfig struct {
      BindAddress string `yaml:"bind_address"`
      BindPort    int    `yaml:"bind_port"`
      VerifySSL   bool   `yaml:"verify_ssl"`
  }
  </code></pre>
  
  <p>I easily unmarshall data from my YAML files like so:</p>
  
  <pre><code>func (config *daemonConfig) getConf() *daemonConfig {
      yamlFile, err := ioutil.ReadFile("config.yaml")
      if err != nil {
          log.Fatal("Unable to open config.yaml:", err)
      }
      err = yaml.Unmarshal(yamlFile, config)
      if err != nil {
          log.Fatal("Failed to unmarshall config.yaml:", err)
      }
      config, err = setDefaults(config)
  
      return config
  }
  </code></pre>
  
  <p>My question is regarding my custom setDefaults function.  If a field isn't provided, like <code>bind_port</code> or <code>bind_address</code>, I'd like to just set them to defaults:</p>
  
  <pre><code>func setDefaults(config *daemonConfig) (*daemonConfig, error) {
      if len(config.BindAddress) &lt;= 0 {
          config.BindAddress = "0.0.0.0"
      }
      if config.BindPort == 0 {
          config.BindPort = 9999
      }
  
      return config, nil
  }
  </code></pre>
  
  <p>You'll note I'm not setting a default to <code>verify_ssl</code>; when <code>yaml</code> unmarshalls this and fails to find the field, it initializes the <code>bool</code> as <code>false</code>, which is precisely the opposite of what I want the default behavior to be.  I'd prefer the user to explicitly set verification of SSL to be off, instead of having it off by default if not specified.  If I have a totally empty <code>config.yaml</code>, and expect the default values to be set, it will always put <code>verify_ssl</code> as false (this log is from elsewhere in the application):</p>
  
  <pre><code>2019/02/19 03:56:08 Currently loaded config: {0.0.0.0 9999 false}
  </code></pre>
  
  <p>How would I go about, if this field is not present in the YAML fields that are being unmarshalled, to checking if the line exists?  I could just read the file manually and check for that parameter first, but I was wondering if there's a more elegant way with what I have; otherwise, I'll just use <code>ioutil</code> and string checking to do it.  Thanks!</p>