LEFT | RIGHT |
(no file at all) | |
1 // run | 1 // run |
2 | 2 |
3 // Copyright 2009 The Go Authors. All rights reserved. | 3 // Copyright 2009 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 // Test behavior of the blank identifier (_). | 7 // Test behavior of the blank identifier (_). |
8 | 8 |
9 package main | 9 package main |
| 10 |
| 11 import "unsafe" |
10 | 12 |
11 import _ "fmt" | 13 import _ "fmt" |
12 | 14 |
13 var call string | 15 var call string |
14 | 16 |
15 type T struct { | 17 type T struct { |
16 _, _, _ int | 18 _, _, _ int |
17 } | 19 } |
18 | 20 |
19 func (T) _() { | 21 func (T) _() { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 97 } |
96 | 98 |
97 sum := 0 | 99 sum := 0 |
98 for s := range ints { | 100 for s := range ints { |
99 sum += s | 101 sum += s |
100 } | 102 } |
101 if sum != 3 { | 103 if sum != 3 { |
102 panic(sum) | 104 panic(sum) |
103 } | 105 } |
104 | 106 |
| 107 type T1 struct{ x, y, z int } |
| 108 t1 := *(*T)(unsafe.Pointer(&T1{1, 2, 3})) |
| 109 t2 := *(*T)(unsafe.Pointer(&T1{4, 5, 6})) |
| 110 if t1 != t2 { |
| 111 panic("T{} != T{}") |
| 112 } |
| 113 |
105 h(a, b) | 114 h(a, b) |
106 » | 115 |
107 m() | 116 m() |
108 } | 117 } |
109 | 118 |
110 type I interface { | 119 type I interface { |
111 M(_ int, y int) | 120 M(_ int, y int) |
112 } | 121 } |
113 | 122 |
114 type TI struct{} | 123 type TI struct{} |
115 | 124 |
116 func (_ TI) M(x int, y int) { | 125 func (_ TI) M(x int, y int) { |
117 if x != y { | 126 if x != y { |
118 println("invalid M call:", x, y) | 127 println("invalid M call:", x, y) |
119 panic("bad M") | 128 panic("bad M") |
120 } | 129 } |
121 } | 130 } |
122 | 131 |
123 var fp = func(_ int, y int) {} | 132 var fp = func(_ int, y int) {} |
124 | 133 |
125 func init() { | 134 func init() { |
126 fp = fp1 | 135 fp = fp1 |
127 } | 136 } |
128 | 137 |
129 func fp1(x, y int) { | 138 func fp1(x, y int) { |
130 if x != y { | 139 if x != y { |
131 println("invalid fp1 call:", x, y) | 140 println("invalid fp1 call:", x, y) |
132 panic("bad fp1") | 141 panic("bad fp1") |
133 } | 142 } |
134 } | 143 } |
135 | 144 |
136 | |
137 func m() { | 145 func m() { |
138 var i I | 146 var i I |
139 » | 147 |
140 i = TI{} | 148 i = TI{} |
141 i.M(1, 1) | 149 i.M(1, 1) |
142 i.M(2, 2) | 150 i.M(2, 2) |
143 » | 151 |
144 fp(1, 1) | 152 fp(1, 1) |
145 fp(2, 2) | 153 fp(2, 2) |
146 } | 154 } |
147 | 155 |
148 // useless but legal | 156 // useless but legal |
149 var _ int = 1 | 157 var _ int = 1 |
150 var _ = 2 | 158 var _ = 2 |
151 var _, _ = 3, 4 | 159 var _, _ = 3, 4 |
152 | 160 |
153 const _ = 3 | 161 const _ = 3 |
154 const _, _ = 4, 5 | 162 const _, _ = 4, 5 |
155 | 163 |
156 type _ int | 164 type _ int |
157 | 165 |
158 func _() { | 166 func _() { |
159 panic("oops") | 167 panic("oops") |
160 } | 168 } |
161 | 169 |
162 func ff() { | 170 func ff() { |
163 var _ int = 1 | 171 var _ int = 1 |
164 } | 172 } |
165 | |
LEFT | RIGHT |