1 | package main |
---|---|
2 | |
3 | // Tests of range loops. |
4 | |
5 | import "fmt" |
6 | |
7 | // Range over string. |
8 | func init() { |
9 | if x := len("Hello, 世界"); x != 13 { // bytes |
10 | panic(x) |
11 | } |
12 | var indices []int |
13 | var runes []rune |
14 | for i, r := range "Hello, 世界" { |
15 | runes = append(runes, r) |
16 | indices = append(indices, i) |
17 | } |
18 | if x := fmt.Sprint(runes); x != "[72 101 108 108 111 44 32 19990 30028]" { |
19 | panic(x) |
20 | } |
21 | if x := fmt.Sprint(indices); x != "[0 1 2 3 4 5 6 7 10]" { |
22 | panic(x) |
23 | } |
24 | s := "" |
25 | for _, r := range runes { |
26 | s += string(r) |
27 | } |
28 | if s != "Hello, 世界" { |
29 | panic(s) |
30 | } |
31 | |
32 | var x int |
33 | for range "Hello, 世界" { |
34 | x++ |
35 | } |
36 | if x != len(indices) { |
37 | panic(x) |
38 | } |
39 | } |
40 | |
41 | // Regression test for range of pointer to named array type. |
42 | func init() { |
43 | type intarr [3]int |
44 | ia := intarr{1, 2, 3} |
45 | var count int |
46 | for _, x := range &ia { |
47 | count += x |
48 | } |
49 | if count != 6 { |
50 | panic(count) |
51 | } |
52 | } |
53 | |
54 | func main() { |
55 | } |
56 |
Members