0

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?

3
  • What is the error you're getting? Commented Nov 10, 2016 at 5:43
  • @Nadh I've already added it to the question, thanks for reminding me Commented Nov 10, 2016 at 6:40
  • As @nothingmuch pointed out in the answer, Go doesn't have structural subtyping. It doesn't have generics either. So, either you can try achieving this by implementing an interface, or implementing getStudentStructs(), getEmployeeStructs() etc. Commented Nov 10, 2016 at 11:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.