In past answers to similar questions, I have seen the stated answers being
that []Fight and []IReset are not the same type, and there is no support
for automatically converting from one to the other, and it will not treat
the slice of concrete types as the slice of IReset interfaces because of a
difference in memory layout.

Basically I think you would need to make([]IReset) and then type assert
when setting the fields:
https://play.golang.org/p/xceK0aoTvQ

Justin


On Tue, Jan 24, 2017 at 2:47 PM hui zhang <fastfad...@gmail.com> wrote:

> check code below, How to use []interface{} in generic programing?
>
> type IReset interface {
>    Reset()
> }
> type Fight struct {
>    hp,mp int
> }
> func (my *Fight) Reset () {
>    my.hp = 0
>    my.mp = 0
> }
> func reset1(x IReset){
>    x.Reset()
> }
> func reset(x []IReset) {
>    for i := range x {
>       x[i].Reset()
>    }
> }
> func  Reset() {
>    arr := make([]Fight,1)
>    arr[0].hp = 100
>    reset1(arr[0])//OK
>    reset(arr)// can not use []Fight as []IReset
> }
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to