title: Is there a more idiomatic approach to creating variables of specific types based on input?
body: <p>In <a href="https://play.golang.org/p/MgL7r2hEWEh" rel="nofollow noreferrer">this playground link</a> I have created a contrived version of my code where I am creating a variable of type X based on an input string. The variable will be one of a handful of types and implement an interface. </p>
  
  <p>The code currently compiles and provides the correct result, however it strikes me as quite verbose and I'm trying to find if there is a shorthand approach to the result I'm achieving. The example has 3 types (dog, cat &amp; bird) that implement the interface (animal), however my actual code will have up to 40 types in this switch statement. </p>
  
  <p>The reason I am using this code is when retrieving results form a DBMS, I'm trying to use a generic load method that, when combined with <a href="https://github.com/jmoiron/sqlx" rel="nofollow noreferrer">sqlx</a>, loads the database table into the correct struct based on the input string. I have entire control over the application and can change the input string to another type if required. </p>
  
  <p>Code from playground link:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  type animal interface {
      call() string
  }
  
  type dog struct {
  }
  
  func (d *dog) call() string {
      return "Woof!"
  }
  
  type cat struct {
  }
  
  func (c *cat) call() string {
      return "Meow!"
  }
  
  type bird struct {
  }
  
  func (c *bird) call() string {
      return "Chirp!"
  }
  
  func main() {
      var animal animal
      animalType := "dog"
      switch animalType{
      case "dog":
          animal = new(dog)
      case "cat":
          animal = new(cat)
      case "bird":
          animal = new(bird)
  </code></pre>