This question already has an answer here:
I have Tag
struct and TableAbstruct
interface like below example.
[Tag struct]
type Tag struct {
Id int `db:"id"`
Name string `db:"Name"`
}
func (tag Tag) Serialize() []string {
...
}
[TableAbstruct interface]
type TableAbstruct interface {
Serialize() []string
}
Xxx()
function returns []TableAbstruct
, but actual type is []Tag
. And below program will work well because Tag
includes TableAbstruct
interface.
func Xxx() []TableAbstruct {
result := []TableAbstruct{}
for i := 0; i < 10; i++ {
table_obj := Tag{}
result = append(result, table_obj)
}
return result
}
But I want to write like below and I couldn't. I think the problem is TypeError. But I couldn't understand why the error has occurred.
func Xxx() []TableAbstruct {
result := []Tag{}
return result
}
</div>