If you don't care about preserving the order of the elements in the slice, 
you can do something like this:
https://play.golang.org/p/6QWxWH-Oj7

The functions I used are documented here: https://golang.org/pkg/reflect/
You may want to read this blog post about 
reflection: https://blog.golang.org/laws-of-reflection
Reflection is the only way to make the function generic.
If you need performance, you can write specialized functions for int, 
float, string and then use reflect to decide which one to call.

On Saturday, September 24, 2016 at 10:10:07 PM UTC+2, Lax Clarke wrote:
>
>
>
> On Friday, September 23, 2016 at 12:30:36 AM UTC-4, Dave Cheney wrote:
>>
>> There are two ways, one is to use reflection, the other would be to use 
>> unsafe to convert the slice value into another structure then extract the 
>> length field. 
>>
>> Assuming that you could write a Len function as you specified, what would 
>> you do next? What problem are you trying to solve?
>>
>
> Ok, I'm sorry for not stating actual problem.
> I'm trying to remove duplicates from slices (of various types, strings, 
> int, float is enough for now).
> This requires me to create a map from that type to boolean, for example, 
> as part of the algorithm.
>
> I cannot figure out how to make such a function work for all types (note 
> I'm coming from C++ and Java where I knew how to do 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.

Reply via email to