EDIT This was due to a stupid error in my part, but the question is worth keeping around in case someone else does this.
I would hope that this works:
var xs []uint8
var x uint8
for x = range xs {
}
But I get the error:
cannot assign type int to x (type uint8) in range
i.e. (as I understand it) the range, even though it is operating on a slice of uint8, is trying to use int as the iterated value.
I have looked through the language spec, the relevant bit:
Range expression 1st value 2nd value (if 2nd variable is present)
array or slice a [n]E, *[n]E, or []E index i int a[i] E
so I would expect it to be E the ‘parameterised type’*, in this case uint8 not int.
Have I grasped onto the wrong end of the stick? Is there a bit of documentation that I missed? Or can I really not iterate over a uint8 slice with a uint8 variable?
*I know it’s not really a generic parameterised type.
rangereturns an index into a collection and possibly the value at that position:sample code:
output:
P.S.:
Perhaps a look at Javascript will make Go’s approach less strange: