可以在Go函数中返回结构的动态数组吗?

问题描述:

Apparently, I want to return an array of struct based on the function parameter (getOccupationStructs function) in order to keep DRY (not using if else in every other functions), but it seems impossible to do, so here is my errors:

 cannot use []Student literal (type []Student) as type []struct {} in
   return argument
 cannot use []Employee literal (type []Employee ) as type []struct {} in
   return argument

and here is my 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)
    }
}

Is there any valid workaround to cope this problem?

显然,我想基于函数参数( getOc​​cupationStructs函数 strong >)以保持DRY(不要在其他所有函数中使用if),但这似乎是不可能的,所以这是我的错误: p>

 无法使用[] Student 文字(类型[] Student)作为类型[] struct {} in 
返回参数
不能使用[]雇员文字(类型[[Employee])作为类型[] struct {} in 
返回参数
  code  >  pre> 
 
 

,这是我的代码: p>

 包main 
 
import(
“ fmt” 
“ time” \  n 
“ github.com/jinzhu/gorm"
 _” github.com/jinzhu/gorm/dialects/postgres"
)

type Human struct {
 ID uint`gorm:“ primary_key” gorm:  “ column:_id” json:“ _ id”`
名称字符串`gorm:“ column:name” json:“ name”`
 Age int`gorm:“ column:age” json:“ age”`
电话 字符串`gorm:“ column:phone” json:“ phone”`
} 
 
type Studen  t struct {
 Human 
学校字符串`gorm:“ column:school” json:“ school”`
贷款float32`gorm:“ column:loan” json:“ loan”`
} 
 
type员工 struct {
 Human 
公司字符串`gorm:“ column:company” json:“ company”`
 Money float32`gorm:“ column:money” json:“ money”`
} 
 
func getOc​​cupationStructs( 职业字符串)[] struct {} {
切换职业{
案例“ student”:
返回[] main.Student {} 
案例“ employee”:
返回[] main.Employee {} 
 默认值:
返回[] main.Student {} 
} 
} 
 
func firstFunction(){
m:= getOc​​cupationStructs(“ student”)
对于_,值:=范围m {
 fmt  .Println(“嗨,我的名字是” + value.Name +“,我的学校是” + value.School)
} 
} 
 
func secondFunction(){
m:= getOc​​cupationStructs(“ employee”)\  n表示_,值:=范围m {
 fmt.Println(“嗨,我的名字是” + value.Name +“,我的公司是” + value.Company)
} 
} 
  code>   pre> 
 
 

是否有有效的解决方法来解决此问题? p> div>

Go doesn't have structural subtyping, so to get polymorphism you'll need to use an interface.

Define an interface that all struct types implement, it can even be private, like interface embedsHuman { Name() string }, and then return []embedsHuman.

Alternatively, restructure your schema or only the Go representation of it as something less hierarchical (perhaps humans can have many roles?), so that it doesn't clash with Go's type system.