| // Copyright 2015 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // Ensure that zeroing range loops have the requisite side-effects. |
| // When n == 0, i is untouched by the range loop. |
| // Picking an initial value of -1 for i makes the |
| // "want" calculation below correct in all cases. |
| if want := n - 1; i != want { |
| fmt.Printf("index after range with side-effect = %d want %d\n", i, want) |
| // i is shadowed here, so its value should be unchanged. |
| if want := n + 1; i != want { |
| fmt.Printf("index after range without side-effect = %d want %d\n", i, want) |
| // Index variable whose evaluation has side-effects |
| if want := n * 2; x != want { |
| fmt.Printf("index function calls = %d want %d\n", x, want) |
| // Range expression whose evaluation has side-effects |
| if want := n + 1; x != n+1 { |
| fmt.Printf("range expr function calls = %d want %d\n", x, want) |