Skip to content

Instantly share code, notes, and snippets.

@gregworley
Created April 28, 2011 16:54
Show Gist options
  • Save gregworley/946746 to your computer and use it in GitHub Desktop.
Save gregworley/946746 to your computer and use it in GitHub Desktop.
From Gary Burd's Oauth.go package
var noEscape = [256]bool{
'A': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
'a': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
'0': true, true, true, true, true, true, true, true, true, true,
'-': true,
'.': true,
'_': true,
'~': true,
}
@gregworley
Copy link
Author

From Gary Burd's oauth.go package

This is declaring an array, but why isn't it {'A':true, 'B':true,...'Z':true,...}
Thanks to Gary for explaining this. When initializing Go's composite literals
for array and slice literals the following rules apply:

Each element has an associated integer index marking its position in the array.
An element with a key uses the key as its index; the key must be a constant integer expression.
An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.

so, since the second true has no key it uses the previous element's index plus one, which in this case is 'B'. Pretty cool, and lots faster than writing everything out.

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