How can I iterate on a slice using bit shifts?

Is there a fancy trick where you can use bit shifts to iterate on a slice ? Can anyone remember such an example ?

You can not “bit shift” a slice. The best way to iterate over them is using for i, v := range theSlice.

I already know that. This is not what I am asking.

Then tell us what you are asking…

I am not trying to sound pretentious, but I am looking for a fancy trick where you can use bit shifts to iterate on a slice.

Well, I said you can’t but you said that wasn’t what you are asking…

So can you perhaps elaborate what you mean? Perhaps with an example in another language?

It is certainly in the nature of us developers to search for, and appreciate if other find, fancy solutions to really difficult, complex, and/or complicated problems.

This being said, Go is a language that primarily supports, and promotes, writing boring code. If you look at code others wrote, or even code you wrote yourself a year ago, you’d be happy to look at readable code rather than fancy one.

The “problem” of iterating over a slice is anything but difficult, and there is a standard solution that everyone should go with, for the sake of readability.

So if you search for a “fancy” way of solving a trivial task, you need to have a very good reason for not going with the standard solution.

(See also: Are You a Hacker or a Designer?)


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.