Created
March 2, 2022 08:03
-
-
Save suncle1993/c48b2a3acfb67854ee58fe9e89d7c3f1 to your computer and use it in GitHub Desktop.
测试string的并发写panic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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