Re: [go-nuts] Re: pass interface
Last but not least, for debugging a value x, this gets you type and value quickly: fmt.Printf("type is %T and value is %#v\n", x, x) On Monday, December 10, 2018 at 8:14:47 PM UTC-6, Mark Volkmann wrote: > > 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)// > fmt.Println("method count is", value.NumMethod()) // 0, Why not 3? > } > > func main() { > var ptr *Shape > ReportInterface(ptr) > } > > -- 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.
Re: [go-nuts] Re: pass interface
Ah! No, that is not possible, because interfaces (and other types) are not first-class objects in Go like they are in some other languages (such as Python). You can work around it, to some extent, by passing a zero-valued instance of such an interface (e.g. PrintInterface(A{})) and using some reflection magics to do what you need based on the reflect.Type and similar. That's how a number of libraries like Resty determine how to e.g. deserialize JSON to a specific type requested by users. It's got its warts, but it works. - Dave > On Dec 10, 2018, at 9:53 AM, 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 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. -- 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.
Re: [go-nuts] Re: pass interface
Nice is a very strong word. This is the alternative approach that doesn't require a value: https://play.golang.org/p/FoA-GHcr56s (now to the whole list) On Tue, 2018-12-11 at 10:39 +0800, Huiqiang Li wrote: > Nice! i think this is the right answer. > > Dan Kortschak 于2018年12月11日周二 上午10:34写道: > > > > > https://play.golang.org/p/VWPb_AcgUrl -- 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.
Re: [go-nuts] Re: pass interface
Thanks so much Dan! --- R. Mark Volkmann Object Computing, Inc. > On Dec 10, 2018, at 8:34 PM, Dan Kortschak wrote: > > https://play.golang.org/p/VWPb_AcgUrl > >> On Mon, 2018-12-10 at 20:14 -0600, Mark Volkmann wrote: >> 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)// >> 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 >> 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 >>> com> 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. > 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().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. >> 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 golang-nuts...@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. > > -- > 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. > -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Re: pass interface
https://play.golang.org/p/VWPb_AcgUrl On Mon, 2018-12-10 at 20:14 -0600, Mark Volkmann wrote: > 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)// > 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 > 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 > > com> > > > 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. > > > > 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().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. > > > > > 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 golang-nuts...@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. > > > > > > > > -- > > > > 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. > > > > > > > -- > > > 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Re: pass interface
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)// 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 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 > > 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. 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().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. 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 golang-nuts...@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. > > > > > > -- > > > 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. > > > > > > > -- > > 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Re: pass interface
Well, you can switch on a type, so you would think the case expression might be able to be used elsewhere. Since the types can be created at runtime via reflect it would seem you should be able to get a reference to the compile time type definition as well. Seems logical to me. > On Dec 10, 2018, at 4:34 PM, Dan Kortschak wrote: > > Oh! Yeah, that's never going to work. How could it? > >> On Mon, 2018-12-10 at 13:43 -0800, Tyler Compton wrote: >> If my interpretation of the question is correct, I think it boils >> down to >> whether or not it's possible to get the reflect.Type of a type in >> order to >> pass it to a function without first creating an instance of that >> type. I >> don't think it's possible but I would be interested to hear from >> someone >> who knows more. >> >> On Mon, Dec 10, 2018 at 1:28 PM Dan Kortschak >> 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 >>> com> 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. > 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().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. >> 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 golang-nuts...@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. > > -- > 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. > -- 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 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
Re: [go-nuts] Re: pass interface
Oh! Yeah, that's never going to work. How could it? On Mon, 2018-12-10 at 13:43 -0800, Tyler Compton wrote: > If my interpretation of the question is correct, I think it boils > down to > whether or not it's possible to get the reflect.Type of a type in > order to > pass it to a function without first creating an instance of that > type. I > don't think it's possible but I would be interested to hear from > someone > who knows more. > > On Mon, Dec 10, 2018 at 1:28 PM Dan Kortschak > 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 > > com> > > > 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. > > > > 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().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. > > > > > 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 golang-nuts...@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. > > > > > > > > -- > > > > 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. > > > > > > > -- > > > 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 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
Re: [go-nuts] Re: pass interface
On Mon, Dec 10, 2018 at 1:43 PM Tyler Compton wrote: > > If my interpretation of the question is correct, I think it boils down to > whether or not it's possible to get the reflect.Type of a type in order to > pass it to a function without first creating an instance of that type. I > don't think it's possible but I would be interested to hear from someone who > knows more. It depends on what you mean by "creating an instance". For example, it suffices to create an instance of a pointer to the type, not an instance of the type itself. But, yes, reflect.TypeOf requires a value of some type. There is no way to get a reflect.Type without starting with some value of some type. Ian > On Mon, Dec 10, 2018 at 1:28 PM Dan Kortschak 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 >> > 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. 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().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. 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 golang-nuts...@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. >> > > >> > > -- >> > > 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. >> > > >> > >> > -- >> > 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 golang-nuts+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. > > > > -- > Tyler Compton > Student of Software Engineering > Arizona State University > > -- > 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
Re: [go-nuts] Re: pass interface
If my interpretation of the question is correct, I think it boils down to whether or not it's possible to get the reflect.Type of a type in order to pass it to a function without first creating an instance of that type. I don't think it's possible but I would be interested to hear from someone who knows more. On Mon, Dec 10, 2018 at 1:28 PM Dan Kortschak 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 > > 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. 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().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. 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 golang-nuts...@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. > > > > > > -- > > > 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. > > > > > > > -- > > 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 golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- Tyler Compton Student of Software Engineering Arizona State University -- 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.
Re: [go-nuts] Re: pass interface
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 > 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. 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().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. 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 golang-nuts...@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. > > > > -- > > 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. > > > > -- > 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Re: pass interface
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 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. 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().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. 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 golang-nuts...@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. > > -- > 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. > -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Re: pass interface
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. 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().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. 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 golang-nuts...@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. -- 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.
Re: [go-nuts] Re: pass interface
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().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. > > 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 golang-nuts...@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.
Re: [go-nuts] Re: pass interface
I mean reflect.Type not a type that is an interface. > On Dec 9, 2018, at 6:53 PM, Space A. 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 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.
[go-nuts] Re: pass interface
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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.