Skip to content

Instantly share code, notes, and snippets.

@suncle1993
Created March 2, 2022 08:03
Show Gist options
  • Save suncle1993/c48b2a3acfb67854ee58fe9e89d7c3f1 to your computer and use it in GitHub Desktop.
Save suncle1993/c48b2a3acfb67854ee58fe9e89d7c3f1 to your computer and use it in GitHub Desktop.
测试string的并发写panic
package main
import (
"fmt"
"time"
)
var (
testString string
)
func main() {
go func() {
for {
tmpString := testString
if tmpString != "0" && tmpString != "11" && tmpString != "" {
fmt.Println(tmpString)
}
}
}()
go func() {
for i := 0; ; i++ {
switch i % 3 {
case 0:
testString = ""
case 1:
testString = "1"
case 2:
testString = "22"
}
time.Sleep(time.Millisecond)
}
}()
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment