Here is some code that shows a part of what I'm trying to do.
https://goplay.space/#8piYtjsqveZ
package main
import (
"fmt"
"reflect"
)
type Shape interface {
Area() float64
Rotate(angle float64)
Translate(x, y float64)
}
func ReportInterface(intfPtr interface{}) {
fmt.Println("type is", reflect.TypeOf(intfPtr)) // *main.Shape
value := reflect.ValueOf(intfPtr)
fmt.Println("value is", value) // <nil>
fmt.Println("method count is", value.NumMethod()) // 0, Why not 3?
}
func main() {
var ptr *Shape
ReportInterface(ptr)
}
On Mon, Dec 10, 2018 at 3:28 PM Dan Kortschak <[email protected]> wrote:
> No, it is possible, but you need to pass the pointer to the interface.
> You can then use reflect to interrogate the interface value.
>
> The bigger question, and one that would help here would be what is it
> that you are actually trying to achieve.
>
> On Mon, 2018-12-10 at 08:53 -0600, Mark Volkmann wrote:
> > Yes, this is what I'm trying to do!
> > Perhaps this is not possible.
> >
> > On Sun, Dec 9, 2018 at 10:34 PM Robert Engels <[email protected]>
> > wrote:
> >
> > >
> > > I think what the OP wants is:
> > >
> > > type A interface{}
> > > type B interface{}
> > >
> > > ...
> > > PrintInterface(A)
> > >
> > > Meaning they want to pass the interface definition to some method.
> > >
> > > At least that’s what I am guessing.
> > >
> > > On Dec 9, 2018, at 9:22 PM, Space A. <[email protected]> wrote:
> > >
> > > reflect/* is a bit tricky. Use pointer to get interface itself.
> > >
> > > package main
> > >
> > > import (
> > > "fmt"
> > > "reflect"
> > > )
> > >
> > > func main() {
> > > test := interface{}("test")
> > > printInterfaceValue(test)
> > > }
> > >
> > > func printInterfaceValue(i interface{}) {
> > > switch testing := i.(type) {
> > > case interface{}:
> > > fmt.Println("is interface, with value:", testing)
> > > case string:
> > > fmt.Println("is not interface")
> > > }
> > >
> > > fmt.Println("reflect.Type is", reflect.TypeOf(&i).Elem())
> > > }
> > >
> > > Output:
> > >
> > > is interface, with value: test
> > > reflect.Type is interface {}
> > >
> > >
> > >
> > >
> > >
> > >
> > > понедельник, 10 декабря 2018 г., 5:05:12 UTC+3 пользователь Robert
> > > Engels
> > > написал:
> > > >
> > > >
> > > > I mean reflect.Type not a type that is an interface.
> > > >
> > > > On Dec 9, 2018, at 6:53 PM, Space A. <[email protected]> wrote:
> > > >
> > > > Of course. When you "pass a value whose type implements the
> > > > interface" as
> > > > an interface argument to a function, you in fact pass an
> > > > *interface*.
> > > >
> > > >
> > > > воскресенье, 9 декабря 2018 г., 23:23:41 UTC+3 пользователь Mark
> > > > Volkmann
> > > > написал:
> > > > >
> > > > >
> > > > > Is it possible to pass an interface to a function in Go? I
> > > > > don’t want to
> > > > > pass a value whose type implements the interface, I want to
> > > > > pass the
> > > > > interface.
> > > > >
> > > > > --
> > > > > R. Mark Volkmann
> > > > > Object Computing, Inc.
> > > > >
> > > > --
> > > > 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 [email protected].
> > > > 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 [email protected].
> > > 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 [email protected].
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> >
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
> >
>
--
R. Mark Volkmann
Object Computing, Inc.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.