Skip to content

Instantly share code, notes, and snippets.

@krashanoff
Created January 19, 2024 23:06
Show Gist options
  • Save krashanoff/4c6ac02a3e62f79c490d8f381c7ddd47 to your computer and use it in GitHub Desktop.
Save krashanoff/4c6ac02a3e62f79c490d8f381c7ddd47 to your computer and use it in GitHub Desktop.
Simple program to demonstrate Go's iterator aliasing.
package main
import "fmt"
type A struct {
b int
}
func main() {
theAs := []A{}
for i := 0; i < 10; i++ {
theAs = append(theAs, A{b: i})
}
fmt.Println(theAs)
done := make(chan bool, 10)
for _, a := range theAs {
go func() {
fmt.Println(a)
done <- true
}()
}
for i := 0; i < 10; i++ {
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment