title: Is it possible to return dynamic array of struct in Go function?
body: <p>Apparently, I want to return an array of struct based on the function parameter (<strong>getOccupationStructs function</strong>) in order to keep DRY (not using if else in every other functions), but it seems impossible to do, so here is my errors: </p>
  
  <pre><code> cannot use []Student literal (type []Student) as type []struct {} in
     return argument
   cannot use []Employee literal (type []Employee ) as type []struct {} in
     return argument
  </code></pre>
  
  <p>and here is my code:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "time"
  
      "github.com/jinzhu/gorm"
      _ "github.com/jinzhu/gorm/dialects/postgres"
  )
  
  type Human struct {
      ID          uint        `gorm:"primary_key" gorm:"column:_id" json:"_id"`
      Name        string      `gorm:"column:name" json:"name"`
      Age         int         `gorm:"column:age" json:"age"`
      Phone       string      `gorm:"column:phone" json:"phone"`
  }
  
  type Student struct {
      Human 
      School      string      `gorm:"column:school" json:"school"`
      Loan        float32     `gorm:"column:loan" json:"loan"`
  }
  
  type Employee struct {
      Human 
      Company     string      `gorm:"column:company" json:"company"`
      Money       float32     `gorm:"column:money" json:"money"`
  }
  
  func getOccupationStructs(occupation string) []struct{} {
      switch occupation {
          case "student":
              return []main.Student{}
          case "employee":
              return []main.Employee{}
          default:
              return []main.Student{}
      }
  }
  
  func firstFunction(){
      m := getOccupationStructs("student")
      for _, value := range m{
          fmt.Println("Hi, my name is "+value.Name+" and my school is "+value.School)
      }
  }
  
  func secondFunction(){
      m := getOccupationStructs("employee")
      for _, value := range m{
          fmt.Println("Hi, my name is "+value.Name+" and my company is "+value.Company)
      }
  }
  </code></pre>
  
  <p>Is there any valid workaround to cope this problem?</p>