| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
// Test order of evaluation of index operations. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
func main() { |
| 12 |
arr := []int{1, 2} |
| 13 |
|
| 14 |
// The spec says that in an assignment statement the operands |
| 15 |
// of all index expressions and pointer indirections on the |
| 16 |
// left, and the expressions on the right, are evaluated in |
| 17 |
// the usual order. The usual order means function calls and |
| 18 |
// channel operations are done first. Then the assignments are |
| 19 |
// carried out one at a time. The operands of an index |
| 20 |
// expression include both the array and the index. So this |
| 21 |
// evaluates as |
| 22 |
// tmp1 := arr |
| 23 |
// tmp2 := len(arr) - 1 |
| 24 |
// tmp3 := len(arr) |
| 25 |
// arr = arr[:tmp3-1] |
| 26 |
// tmp1[tmp2] = 3 |
| 27 |
arr, arr[len(arr)-1] = arr[:len(arr)-1], 3 |
| 28 |
|
| 29 |
if len(arr) != 1 || arr[0] != 1 || arr[:2][1] != 3 { |
| 30 |
panic(arr) |
| 31 |
} |
| 32 |
} |