Am I reading this correctly? The #golang standard library has no built in function to get a slice of keys from a map? You need to iterate every single time and build up an extra list??
Well i don't get it.
Get a list of keys from a map to iterate the keys-list instead of the map, doubles the mem and doubles the work.. I never had the case to need the list of keys of a map 🤔 without do something in a iterative way with it.
Do you have an example for this case ?
If this is a java-thing, then better use java 😆
@martinsstar in this case I am passing in an object with a dynamic set of fields to use in a query and I just need the set of keys to compare against other sets within database indexes.
is it faster to do a map loop each time instead of having this slice? I assumed it's more overhead to iterate the entire map instead of an array of keys. Honestly I just want a high level Set type to do unions and diffs.
@martinsstar good to know. I'll try some benchmarking and profiling. :)
@mauve okay, programmers can not stop thinking about problems...
i do some benchmark of string-lookups in arrays or maps in #golang.
I take the worst case where the first string is not found in the second map/array ( this should only be the case if both objects are completely different )
maps: 41455 ns/op
arrays: 2283822 ns/op
But "iterate" over maps seems a little bit slower ( for 1000000000 ops )
range over maps: 0.0000098 ns/op
range over arrays: 0.0000005 ns/op
@mauve cool would be nice to know if my guess is right.
If two maps need to get compared, i would iterate the first map and look in the second if the key exist. So i guess using of maps will be faster ( because of the key lookup), than to create two arrays of keys and do the lookup there.
But i prefer to use a readable solution over an fast one 😉
Currently i have no time ( maybe tomorrow ) to do an benchmark about this. If you are faster, please inform us about your result 😎 👍