OneBug This week's bugs
Spot the bug Go
1
package main
2
 
3
import "fmt"
4
 
5
func makeGreeters(names []string) []func() string {
6
	msg := ""
7
	fs := make([]func() string, 0, len(names))
8
	for _, n := range names {
9
		msg = "hi " + n
10
		fs = append(fs, func() string { return msg })
11
	}
12
	return fs
13
}
14
 
15
func main() {
16
	fs := makeGreeters([]string{"ana", "bo"})
17
	fmt.Println(fs[0](), fs[1]())  // expected: hi ana hi bo, actual: hi bo hi bo  (!)
18
}

Read the full write-up for this bug (spoilers)