Slice of struct can't be plugged into slice of interface in golang [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
golang: slice of struct != slice of interface it implements?
5 answers
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
}
go
marked as duplicate by Volker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 6:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
golang: slice of struct != slice of interface it implements?
5 answers
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
}
go
marked as duplicate by Volker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 6:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
golang: slice of struct != slice of interface it implements?
5 answers
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
}
go
This question already has an answer here:
golang: slice of struct != slice of interface it implements?
5 answers
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
}
This question already has an answer here:
golang: slice of struct != slice of interface it implements?
5 answers
go
go
asked Nov 9 at 3:46
mk-tool
315
315
marked as duplicate by Volker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 6:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Volker
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 6:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Go does not have any fanciness around slices and types. Put simply, if you say you are going to return TableAbstruct
, you have to return that exactly. So if you want to return a Tag
, you have to create a slice of TableAbstruct
and then go populate it manually:
func Xxx() TableAbstruct {
var returnValue TableAbstruct
for _, t := range result {
returnValue = append(returnValue, t)
}
return returnValue
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Go does not have any fanciness around slices and types. Put simply, if you say you are going to return TableAbstruct
, you have to return that exactly. So if you want to return a Tag
, you have to create a slice of TableAbstruct
and then go populate it manually:
func Xxx() TableAbstruct {
var returnValue TableAbstruct
for _, t := range result {
returnValue = append(returnValue, t)
}
return returnValue
}
add a comment |
up vote
1
down vote
accepted
Go does not have any fanciness around slices and types. Put simply, if you say you are going to return TableAbstruct
, you have to return that exactly. So if you want to return a Tag
, you have to create a slice of TableAbstruct
and then go populate it manually:
func Xxx() TableAbstruct {
var returnValue TableAbstruct
for _, t := range result {
returnValue = append(returnValue, t)
}
return returnValue
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Go does not have any fanciness around slices and types. Put simply, if you say you are going to return TableAbstruct
, you have to return that exactly. So if you want to return a Tag
, you have to create a slice of TableAbstruct
and then go populate it manually:
func Xxx() TableAbstruct {
var returnValue TableAbstruct
for _, t := range result {
returnValue = append(returnValue, t)
}
return returnValue
}
Go does not have any fanciness around slices and types. Put simply, if you say you are going to return TableAbstruct
, you have to return that exactly. So if you want to return a Tag
, you have to create a slice of TableAbstruct
and then go populate it manually:
func Xxx() TableAbstruct {
var returnValue TableAbstruct
for _, t := range result {
returnValue = append(returnValue, t)
}
return returnValue
}
answered Nov 9 at 4:29
poy
5,61753262
5,61753262
add a comment |
add a comment |