...
1go test -bench=Foo -cpuprofile=default.pgo
2go test -bench=Foo -pgo=default.pgo
3! stdout 'FAIL'
4
5-- main_test.go --
6package main
7
8import (
9 "testing"
10)
11
12var a int
13
14func save(x int) {
15 a = x
16}
17
18func foo() {
19 for i := range yield1 {
20 defer save(i)
21 }
22}
23
24func yield1(yield func(int) bool) {
25 yield(1)
26}
27
28func BenchmarkFoo(b *testing.B) {
29 for i := 0; i < b.N; i++ {
30 foo()
31 }
32 if a != 1 {
33 b.Fatalf("a = %d; want 1", a)
34 }
35}
36
37-- go.mod --
38module demo
39
40go 1.24
View as plain text