I was playing around with slices, and I found something that I can't explain. I create a string of total length 10 (index ranges from 0 to 9) and then I create a slice from it specifying one past the max index and it does not panic. It will panic if I go more than one past the last index. What am I not understanding please?
I have investigated with integer slices as well thinking there might be something odd about strings, but it showed the same behavior.
Here is an example: I expected a failure at x:= str[10:]
.
package main
import "fmt"
func main() {
var str = "attribute="
x := str[10:]
fmt.Printf("type of x is %T
", x)
//fmt.Printf("x is a %T and length of x is %d
", x, len(x))
for k, v := range []byte(str) {
fmt.Printf("%d, %x
", k, v)
}
}
Playground: https://play.golang.org/p/Z-3YvTx3-s6
Output:
type of x is string
0, 61
1, 74
2, 74
3, 72
4, 69
5, 62
6, 75
7, 74
8, 65
9, 3d