Re: [go-nuts] Is there a function in standard lib to convert []T to a []interface{}?
I guess the difference is that a builtin COULD be compile-time type safe. It could reject anything that is not a slice or an array, the same way append is type safe. Le mercredi 3 août 2016 17:51:44 UTC+2, Thomas Bushnell, BSG a écrit : > > On Wed, Aug 3, 2016 at 7:36 AM T L> wrote: > >> Often, I need converting a []T to []interface{} to use the []interface as >> a variable length parameter. >> But converting a []T for []interface{} in a for loop is neither clean nor >> efficient. >> > > If there was a builtin that did it, it would simply need to do the same > loop. The memory representation is not the same. > > Thomas > > -- 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] Is there a function in standard lib to convert []T to a []interface{}?
On Thu, Aug 4, 2016 at 3:33 PM, T Lwrote: > > With some special memory optimizations for slice, I think it is possible to > make efficient conversions from []T to []interface. > For example, we don't need to convert every element in []T to interface{}, > we can just use following struct to represent a special []interface{}, > the concrete type of all interface{} values in the []interface{} is the same > one. >> >> type specialInterfaceSlice struct { >> typ *_type >> values []T >> } > That avoids the copy, but makes it impossible to take the address of an interface{} in that []interface{}. -- 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] Is there a function in standard lib to convert []T to a []interface{}?
On Thursday, August 4, 2016 at 12:16:37 AM UTC+8, Ian Lance Taylor wrote: > > On Wed, Aug 3, 2016 at 9:12 AM, T L> wrote: > > > > On Wednesday, August 3, 2016 at 11:46:43 PM UTC+8, Axel Wagner wrote: > >> > >> True, but it would still be just the same loop, it wouldn't actually be > >> significantly faster. And you'd need to put quite some machinery into a > >> pretty rarely used functionality, which means it also wouldn't be > cleaner. > >> > >> The thing is, that the memory representation of []T and []J, with J > being > >> an interface type, is very different form each other and differs also > for > >> each (T, J) pair, AIUI. So you can't really efficiently generalize > this. The > >> only thing it could possibly safe you, is writing the actual loop and > in > >> general go doesn't really do this kind of tradeoff (saving small > amounts of > >> trivial work by complicating the language and -implementation). > > > > > > so the copy buitlin function is not essential? > > Correct. > > Although it is worth noting that, in the absence of significant > compiler optimizations that the gc compiler does not currently > implement, the copy builtin can be much more efficient than the > ordinary user written loop; see > https://golang.org/src/runtime/memmove_amd64.s. That is not true of a > function that converts from []T to []interface. > With some special memory optimizations for slice, I think it is possible to make efficient conversions from []T to []interface. For example, we don't need to convert every element in []T to interface{}, we can just use following struct to represent a special []interface{}, the concrete type of all interface{} values in the []interface{} is the same one. > type specialInterfaceSlice struct { > typ *_type > values []T > } > > > Ian > -- 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] Is there a function in standard lib to convert []T to a []interface{}?
On Wednesday, August 3, 2016 at 6:16:37 PM UTC+2, Ian Lance Taylor wrote: > > On Wed, Aug 3, 2016 at 9:12 AM, T L> wrote: > > > > On Wednesday, August 3, 2016 at 11:46:43 PM UTC+8, Axel Wagner wrote: > >> > >> True, but it would still be just the same loop, it wouldn't actually be > >> significantly faster. And you'd need to put quite some machinery into a > >> pretty rarely used functionality, which means it also wouldn't be > cleaner. > >> > >> The thing is, that the memory representation of []T and []J, with J > being > >> an interface type, is very different form each other and differs also > for > >> each (T, J) pair, AIUI. So you can't really efficiently generalize > this. The > >> only thing it could possibly safe you, is writing the actual loop and > in > >> general go doesn't really do this kind of tradeoff (saving small > amounts of > >> trivial work by complicating the language and -implementation). > > > > > > so the copy buitlin function is not essential? > > Correct. > > Although it is worth noting that, in the absence of significant > compiler optimizations that the gc compiler does not currently > implement, the copy builtin can be much more efficient than the > ordinary user written loop; see > https://golang.org/src/runtime/memmove_amd64.s. That is not true of a > function that converts from []T to []interface. > > Ian > Enabling user to do conversion from []*Tto[]*U (the slices contain pointers) would be easy, doing a slight modification in the compilers. We did tricks like this already in modified versions of go Enabling user to do conversion from []T (T is arbitrary not just fixed size type) to []interface would be possible with a deep modification involving linkers and other stuff (that I don't understand very well). Dealing with []T when T is an interface would be nightmare difficult and I don't even know where would I begin i believe in go experts they are more than capable to deal with this -- 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] Is there a function in standard lib to convert []T to a []interface{}?
On Wednesday, August 3, 2016 at 11:46:43 PM UTC+8, Axel Wagner wrote: > > True, but it would still be just the same loop, it wouldn't actually be > significantly faster. And you'd need to put quite some machinery into a > pretty rarely used functionality, which means it also wouldn't be cleaner. > > The thing is, that the memory representation of []T and []J, with J being > an interface type, is very different form each other and differs also for > each (T, J) pair, AIUI. So you can't really efficiently generalize this. > The only thing it could possibly safe you, is writing the actual loop and > in general go doesn't really do this kind of tradeoff (saving small amounts > of trivial work by complicating the language and -implementation). > so the copy buitlin function is not essential? > > On Wed, Aug 3, 2016 at 5:19 PM, T L> wrote: > >> >> >> On Wednesday, August 3, 2016 at 10:54:29 PM UTC+8, Axel Wagner wrote: >>> >>> Why is converting it in a loop "neither clean nor efficient"? It seems >>> to be both to me: a) It's clean, as it's type-safe, so much less can go >>> wrong and it's obvious what it does and b) it's efficient, becaue a >>> func([]T) []interface{} would need to use reflection, just to also have the >>> same loop (but a less efficient one, as every operation would need to >>> reflect). So, writing a loop would, in fact, be *more* efficient and clean >>> than a function. >>> >> >> If the functionality is provided by builltin package, the reflection is >> not needed, just like the copy builtin function. >> >> >>> >>> On Wed, Aug 3, 2016 at 4:35 PM, T L wrote: >>> Often, I need converting a []T to []interface{} to use the []interface as a variable length parameter. But converting a []T for []interface{} in a for loop is neither clean nor efficient. So is there a function in standard lib to convert []T to a []interface{}? -- 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...@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] Is there a function in standard lib to convert []T to a []interface{}?
https://github.com/golang/go/issues/15209 On Wednesday, August 3, 2016 at 11:20:01 AM UTC-4, T L wrote: > > > > On Wednesday, August 3, 2016 at 10:53:34 PM UTC+8, Jessta wrote: >> >> On 4 Aug 2016 12:36 a.m., "T L"wrote: >> > >> > Often, I need converting a []T to []interface{} to use the []interface >> as a variable length parameter. >> > But converting a []T for []interface{} in a for loop is neither clean >> nor efficient. >> > >> > So is there a function in standard lib to convert []T to a >> []interface{}? >> > >> >> There is no function and it's not possible to define one in Go. >> >> Define your own function for your specific type or avoid []interface{} if >> you can. Usually what you want is to put a []T in an interface{} and use >> reflect instead. >> > > fmt.Println needs a []interface{} parameter. > > -- 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] Is there a function in standard lib to convert []T to a []interface{}?
You probably don't want to actually call fmt.Println(s...) with s an []interface{}. Just do fmt.Print(s) instead. On Wed, Aug 3, 2016 at 5:20 PM, T Lwrote: > > > On Wednesday, August 3, 2016 at 10:53:34 PM UTC+8, Jessta wrote: >> >> On 4 Aug 2016 12:36 a.m., "T L" wrote: >> > >> > Often, I need converting a []T to []interface{} to use the []interface >> as a variable length parameter. >> > But converting a []T for []interface{} in a for loop is neither clean >> nor efficient. >> > >> > So is there a function in standard lib to convert []T to a >> []interface{}? >> > >> >> There is no function and it's not possible to define one in Go. >> >> Define your own function for your specific type or avoid []interface{} if >> you can. Usually what you want is to put a []T in an interface{} and use >> reflect instead. >> > > fmt.Println needs a []interface{} parameter. > > > -- > 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] Is there a function in standard lib to convert []T to a []interface{}?
On Wed, Aug 3, 2016 at 7:36 AM T Lwrote: > Often, I need converting a []T to []interface{} to use the []interface as > a variable length parameter. > But converting a []T for []interface{} in a for loop is neither clean nor > efficient. > If there was a builtin that did it, it would simply need to do the same loop. The memory representation is not the same. Thomas -- 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] Is there a function in standard lib to convert []T to a []interface{}?
Don't confuse variadic arguments with slice arguments, by the way. On Wed, Aug 3, 2016 at 8:20 AM T Lwrote: > > > On Wednesday, August 3, 2016 at 10:53:34 PM UTC+8, Jessta wrote: > >> On 4 Aug 2016 12:36 a.m., "T L" wrote: >> > >> > Often, I need converting a []T to []interface{} to use the []interface >> as a variable length parameter. >> > But converting a []T for []interface{} in a for loop is neither clean >> nor efficient. >> > >> > So is there a function in standard lib to convert []T to a >> []interface{}? >> > >> >> There is no function and it's not possible to define one in Go. >> >> Define your own function for your specific type or avoid []interface{} if >> you can. Usually what you want is to put a []T in an interface{} and use >> reflect instead. >> > > fmt.Println needs a []interface{} parameter. > > > -- > 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] Is there a function in standard lib to convert []T to a []interface{}?
True, but it would still be just the same loop, it wouldn't actually be significantly faster. And you'd need to put quite some machinery into a pretty rarely used functionality, which means it also wouldn't be cleaner. The thing is, that the memory representation of []T and []J, with J being an interface type, is very different form each other and differs also for each (T, J) pair, AIUI. So you can't really efficiently generalize this. The only thing it could possibly safe you, is writing the actual loop and in general go doesn't really do this kind of tradeoff (saving small amounts of trivial work by complicating the language and -implementation). On Wed, Aug 3, 2016 at 5:19 PM, T Lwrote: > > > On Wednesday, August 3, 2016 at 10:54:29 PM UTC+8, Axel Wagner wrote: >> >> Why is converting it in a loop "neither clean nor efficient"? It seems to >> be both to me: a) It's clean, as it's type-safe, so much less can go wrong >> and it's obvious what it does and b) it's efficient, becaue a func([]T) >> []interface{} would need to use reflection, just to also have the same loop >> (but a less efficient one, as every operation would need to reflect). So, >> writing a loop would, in fact, be *more* efficient and clean than a >> function. >> > > If the functionality is provided by builltin package, the reflection is > not needed, just like the copy builtin function. > > >> >> On Wed, Aug 3, 2016 at 4:35 PM, T L wrote: >> >>> Often, I need converting a []T to []interface{} to use the []interface >>> as a variable length parameter. >>> But converting a []T for []interface{} in a for loop is neither clean >>> nor efficient. >>> >>> So is there a function in standard lib to convert []T to a []interface{}? >>> >>> -- >>> 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] Is there a function in standard lib to convert []T to a []interface{}?
On 4 Aug 2016 12:36 a.m., "T L"wrote: > > Often, I need converting a []T to []interface{} to use the []interface as a variable length parameter. > But converting a []T for []interface{} in a for loop is neither clean nor efficient. > > So is there a function in standard lib to convert []T to a []interface{}? > There is no function and it's not possible to define one in Go. Define your own function for your specific type or avoid []interface{} if you can. Usually what you want is to put a []T in an interface{} and use reflect instead. -- 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] Is there a function in standard lib to convert []T to a []interface{}?
Often, I need converting a []T to []interface{} to use the []interface as a variable length parameter. But converting a []T for []interface{} in a for loop is neither clean nor efficient. So is there a function in standard lib to convert []T to a []interface{}? -- 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.