英文:
Is it possible to return dynamic array of struct in Go function?
问题
显然,我想根据函数参数(getOccupationStructs函数)返回一个结构体数组,以保持DRY(不在每个其他函数中使用if else),但似乎无法实现,所以这是我的错误:
cannot use []Student literal (type []Student) as type []struct {} in
return argument
cannot use []Employee literal (type []Employee ) as type []struct {} in
return argument
这是我的代码:
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)
}
}
有没有有效的解决方法来解决这个问题?
英文:
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?
答案1
得分: 1
Go语言没有结构子类型化,所以要实现多态性,你需要使用接口。
定义一个所有结构类型都实现的接口,它甚至可以是私有的,比如 interface embedsHuman { Name() string }
,然后返回 []embedsHuman
。
或者,重新构建你的模式或者只将其Go表示形式重构为不那么分层的结构(也许人可以有多个角色?),这样就不会与Go的类型系统冲突。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论