github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/loopvar/testdata/for_esc_method.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  type I int
    13  
    14  func (x *I) method() int {
    15  	return int(*x)
    16  }
    17  
    18  func main() {
    19  	sum := 0
    20  	var is []func() int
    21  	for i := I(0); int(i) < 10; i++ {
    22  		for j := 0; j < 10; j++ {
    23  			if int(i) == j { // 10 skips
    24  				continue
    25  			}
    26  			sum++
    27  		}
    28  		if i&1 == 0 {
    29  			is = append(is, i.method)
    30  		}
    31  	}
    32  
    33  	bug := false
    34  	if sum != 100-10 {
    35  		fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
    36  		bug = true
    37  	}
    38  	sum = 0
    39  	for _, m := range is {
    40  		sum += m()
    41  	}
    42  	if sum != 2+4+6+8 {
    43  		fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
    44  		bug = true
    45  	}
    46  	if !bug {
    47  		fmt.Printf("PASS\n")
    48  	} else {
    49  		os.Exit(11)
    50  	}
    51  }