测试代码: https://play.golang.org/p/7D3nEYF3UFX
// #1
f := func() bool { return false }
switch f()
{
case true:
println("true")
case false:
println("false")
}
// #2
arr := [3]int{1,2,3}
arrPtrOfLen := &arr
for i := range arrPtrOfLen { println(i) }
arr2 := []int{1,2,3}
arrPtrOfVar := &arr2
for j := range arrPtrOfVar { println(j) }
//// arrPtrOfVar compile error: cannot range over data (type *[]int)
// #3
for k := range (*[3]int) (nil) {
println(k)
}
1
zzn 2019-05-23 23:28:21 +08:00
这些标准里都写的清清楚楚吧
1. > A missing switch expression is equivalent to the boolean value true. https://golang.org/ref/spec#Switch_statements 2. > The expression on the right in the "range" clause is called the range expression, which may be an array, pointer to an array, slice, string, map, or channel permitting receive operations. https://golang.org/ref/spec#RangeClause 3. 就在 RangeClause 的例子里 |
2
liulaomo 2019-05-24 06:55:04 +08:00
|
3
Weixiao0725 OP |