title: Rewrite a variable type on the basis if condition in golang
body: <p>I come from a python background and a newbie in Golang.
  I started to write some code in Golang and came across an issue of setting a data type on the basis of if condition.
  On working with the Govmomi module, I came across a <a href="https://github.com/vmware/govmomi/blob/15d36b27b1375b9f85a86cb67902cb41645e168c/view/container_view.go#L39" rel="nofollow noreferrer">Retrieve</a> method that accepts the last parameter as an interface.</p>
  <p>In my script, I'm checking for an argument like the following</p>
  <pre><code>    var hss []mo.HostSystem
      var ds []mo.Datastore
  
      if command == &quot;VMFS&quot; {
          err = v.Retrieve(ctx, []string{commandChoices[command]}, []string{&quot;name&quot;, &quot;summary&quot;}, &amp;ds)
          if err != nil {
              log.Fatal(err)
          }
  
          for _, host := range ds {
              fmt.Println(host.Name)
          }
      } else {
          err = v.Retrieve(ctx, []string{commandChoices[command]}, []string{&quot;name&quot;, &quot;summary&quot;}, &amp;hss)
          if err != nil {
              log.Fatal(err)
          }
  
          for _, host := range hss {
              fmt.Println(host.Name)
      }
  </code></pre>
  <p>More of this code is boilerplate code that I wanted to avoid but not sure how to set a data type on the basis of an if condition. I'm looking for something like</p>
  <pre><code>if command == &quot;VMFS&quot; {
  dss = []mo.Datasore
  } else {
  dss = []mo.HostSystem
  }
  </code></pre>
  <p>And then pass dss as the last argument to <em>Retrieve</em> method.
  Any help is really appreciated.</p>