Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created March 9, 2022 08:14
Show Gist options
  • Save tebeka/82e516f0624cfb11cec233916ada64d9 to your computer and use it in GitHub Desktop.
Save tebeka/82e516f0624cfb11cec233916ada64d9 to your computer and use it in GitHub Desktop.

Seeking Permission or Begging Forgiveness?

A Go Brain Teaser

What do you think the following program will print?

https://gist.github.com/cd76f0d470d06005166315e3f1a81845

This program will print: 4.

When you use iota in a const group, the values of the constants will advance according to the first operation. The << operator is "left shift". It moves the bits of a number to the left, basically multiplying it by power of 2.

For example, if we have the number 5, which (on byte, which is 8 bits) is 00000101. When you write 5 << 1 all the bits are shifted one place to the left. Making it 00001010 which is 10=5*2. When you write 5 << 2 the bits are shifted two places to the left, making it 00010100 or 20=5*4.

The first value, Read is 1<<0 which is 1. The next value, Write is 1<<1 which is 2. Finally, Execute is 1<<2 which is 4.

iota is Go's version of enumeration, if you're coming from other language, you're probably used to enumerated types being printed as strings. In Go, you can implement that fmt.Stringer.

https://gist.github.com/8ae49733c68b61d799631911310bb101

One you do that, the code above will print execute.

Note that FilePerm can be any uint16 value, this is why we have the final clause in the String method. Why do you think the comment says "don't use %s or %v"?

Using values that are power of 2 enables you to use bitwise operations or bitmasks. For example:

https://gist.github.com/73c1bdb519738be39cb0070265e2357f

Bitmasks are a compact way of representing flags or options. If instead of using a bitmask, you'll use a struct like:

https://gist.github.com/7bd4f81a97a47c01640ac94912a543be

Then it'll be 3 times as big:

https://gist.github.com/51594251dbad74a99af77f87818c3b0f

2 byte difference might not seem a lot, but as Dave Cheney says: "A few bytes here, a few there, pretty soon you’re talking real memory."

I encourage you to try out bitmask next time you write some code, I'm sure you'll find them useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment