FAQ
Please see the code. http://play.golang.org/p/d76ZNwBb-c

I am testing whether some values implement interface using reflection. it
works in fun1, and not in fun2. It is mainly about how to reflect on an
interface value of slice of values, or a slice of interface values. How can
I do it properly in fun2. code below:

http://play.golang.org/p/d76ZNwBb-c

package main

import (
"fmt"
"reflect"
)

type Ider interface {
GetId() string
}

type tp struct {
Id string
}

func (s *tp) GetId() string {
return s.Id
}

func fun1(l int, d interface{}) {
v := reflect.ValueOf(d)
for i := 0; i < l; i++ {
elm := v.Index(i)
elmInr := elm.Interface()

if _, ok := elmInr.(Ider); ok {
fmt.Println("fun1: Ider - ok")
}

}

}

func fun2(d ...interface{}) {
l := len(d)
for i := 0; i < l; i++ {
v := reflect.ValueOf(d[i])
elmInr := v.Interface()

if _, ok := elmInr.(Ider); ok {
fmt.Println("fun2: Ider - ok")
}

}

}

func main() {
t1 := &tp{"a"}
t2 := &tp{"b"}
t3 := &tp{"c"}
t := []*tp{
t1, t2, t3,
}
fun1(len(t), t)
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Bsr at Feb 8, 2014 at 12:51 pm
    Sorry the code was incomplete. please see the updated one
    here http://play.golang.org/p/PnNDbOxyAt

    fun2(t1, t2, t3) -- works
    fun2(t) -- doesn't work

    I understand they are different, but is there a way second to make it work.
    On Saturday, February 8, 2014 7:44:14 AM UTC-5, bsr wrote:

    Please see the code. http://play.golang.org/p/d76ZNwBb-c

    I am testing whether some values implement interface using reflection. it
    works in fun1, and not in fun2. It is mainly about how to reflect on an
    interface value of slice of values, or a slice of interface values. How can
    I do it properly in fun2. code below:

    http://play.golang.org/p/d76ZNwBb-c

    package main

    import (
    "fmt"
    "reflect"
    )

    type Ider interface {
    GetId() string
    }

    type tp struct {
    Id string
    }

    func (s *tp) GetId() string {
    return s.Id
    }

    func fun1(l int, d interface{}) {
    v := reflect.ValueOf(d)
    for i := 0; i < l; i++ {
    elm := v.Index(i)
    elmInr := elm.Interface()

    if _, ok := elmInr.(Ider); ok {
    fmt.Println("fun1: Ider - ok")
    }

    }

    }

    func fun2(d ...interface{}) {
    l := len(d)
    for i := 0; i < l; i++ {
    v := reflect.ValueOf(d[i])
    elmInr := v.Interface()

    if _, ok := elmInr.(Ider); ok {
    fmt.Println("fun2: Ider - ok")
    }

    }

    }

    func main() {
    t1 := &tp{"a"}
    t2 := &tp{"b"}
    t3 := &tp{"c"}
    t := []*tp{
    t1, t2, t3,
    }
    fun1(len(t), t)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bsr at Feb 8, 2014 at 12:55 pm
    also, mapping it into a []interface{} works. is this the only way

    http://play.golang.org/p/4mJOhyWZtv


    On Saturday, February 8, 2014 7:51:35 AM UTC-5, bsr wrote:

    Sorry the code was incomplete. please see the updated one here
    http://play.golang.org/p/PnNDbOxyAt

    fun2(t1, t2, t3) -- works
    fun2(t) -- doesn't work

    I understand they are different, but is there a way second to make it work.
    On Saturday, February 8, 2014 7:44:14 AM UTC-5, bsr wrote:

    Please see the code. http://play.golang.org/p/d76ZNwBb-c

    I am testing whether some values implement interface using reflection. it
    works in fun1, and not in fun2. It is mainly about how to reflect on an
    interface value of slice of values, or a slice of interface values. How can
    I do it properly in fun2. code below:

    http://play.golang.org/p/d76ZNwBb-c

    package main

    import (
    "fmt"
    "reflect"
    )

    type Ider interface {
    GetId() string
    }

    type tp struct {
    Id string
    }

    func (s *tp) GetId() string {
    return s.Id
    }

    func fun1(l int, d interface{}) {
    v := reflect.ValueOf(d)
    for i := 0; i < l; i++ {
    elm := v.Index(i)
    elmInr := elm.Interface()

    if _, ok := elmInr.(Ider); ok {
    fmt.Println("fun1: Ider - ok")
    }

    }

    }

    func fun2(d ...interface{}) {
    l := len(d)
    for i := 0; i < l; i++ {
    v := reflect.ValueOf(d[i])
    elmInr := v.Interface()

    if _, ok := elmInr.(Ider); ok {
    fmt.Println("fun2: Ider - ok")
    }

    }

    }

    func main() {
    t1 := &tp{"a"}
    t2 := &tp{"b"}
    t3 := &tp{"c"}
    t := []*tp{
    t1, t2, t3,
    }
    fun1(len(t), t)
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 8, '14 at 12:44p
activeFeb 8, '14 at 12:55p
posts3
users1
websitegolang.org

1 user in discussion

Bsr: 3 posts

People

Translate

site design / logo © 2022 Grokbase