4 ms·
Here's a more idiomatic go version: http://play.golang.org/p/oH850AdLuQ http://play.golang.org/p/oH850AdLuQ what you don't realize (apart from the fact that g
by f2f 14y ago
Here's a more idiomatic go version:
http://play.golang.org/p/oH850AdLuQ http://play.golang.org/p/oH850AdLuQ
what you don't realize (apart from the fact that go doesn't terminate lines with semicolons since 2009) is that you don't need to create a copy that's typed []interface{} because []X already satisfies interface{} and you can use reflection to handle it. if a seasoned Go programmer wanted to write a somewhat generic map function they'd do something similar to this:
http://play.golang.org/p/qM8sBNcdwY http://play.golang.org/p/qM8sBNcdwY
It doesn't look pretty and you're right, most go programmers don't have to write code like this. It is much more common to define a common interface for something that's map-able and expect callers to implement that. examples in sort.Interface.
- gnuvince 14y agoIn your more idiomatic version, you actually went ahead and changed the declaration of words to be []interface{}, but you may not be in control of the type of words (for instance if it's the return value of a function). This is why I did the explicit conversion. As for the reflection, it's pretty ugly. Also, what about semi-colons?
- f2f 14y agoi understand why you did the explicit conversion, the idea i was trying to relay is why i undid it... nevermind the semicolons, 'twas a joke :)