github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/loopvar/testdata/range_esc_address.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  var ints = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    13  
    14  func main() {
    15  	sum := 0
    16  	var is []*int
    17  	for _, i := range ints {
    18  		for j := 0; j < 10; j++ {
    19  			if i == j { // 10 skips
    20  				continue
    21  			}
    22  			sum++
    23  		}
    24  		if i&1 == 0 {
    25  			is = append(is, &i)
    26  		}
    27  	}
    28  
    29  	bug := false
    30  	if sum != 100-10 {
    31  		fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
    32  		bug = true
    33  	}
    34  	sum = 0
    35  	for _, pi := range is {
    36  		sum += *pi
    37  	}
    38  	if sum != 2+4+6+8 {
    39  		fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
    40  		bug = true
    41  	}
    42  	if !bug {
    43  		fmt.Printf("PASS\n")
    44  	} else {
    45  		os.Exit(11)
    46  	}
    47  }