Skip to content

Instantly share code, notes, and snippets.

@nikolaydubina
Created February 24, 2023 03:37
Show Gist options
  • Save nikolaydubina/db2e98ba2398e57ebb975ff20abc116c to your computer and use it in GitHub Desktop.
Save nikolaydubina/db2e98ba2398e57ebb975ff20abc116c to your computer and use it in GitHub Desktop.
// https://go.dev/play/p/PX6EWqZsXR4
// race condition in interface cast 😵‍💫
package main
import "fmt"
type I interface{ method() string }
type A struct{ s string }
type B struct {
u uint32
s string
}
func (a A) method() string { return a.s }
func (b B) method() string { return b.s }
func main() {
a := A{s: "..."}
b := B{u: ^uint32(0), s: "..."}
var i I = a
go func() {
for {
i = a
i = b
}
}()
for {
if s := i.method(); len(s) > 3 {
fmt.Printf("len(s)=%d\n", len(s))
fmt.Println(s)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment