You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var v string
func TestPanic(t *testing.T) {
var slice []string
go func() {
slice = append(slice, "hello")
}()
for len(slice) <= 0 {
}
v = slice[0]
}
Run the test as follows:
go test -run TestPanic -count=1000
What did you expect to see?
Disregarding the data race in the test above, I expected to be able to access the first element in the slice if len(slice) returns a number greater than zero
What did you see instead?
There was a panic about nil pointer dereference although that access can only happen if the length of slice is greater than zero.
It doesn't really matter, the program is racy and all kind of corruption can happen. It's not possible to make consistency guarantees on a racy program.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes (go version go1.15.3 linux/amd64)
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
Disregarding the data race in the test above, I expected to be able to access the first element in the slice if
len(slice)
returns a number greater than zeroWhat did you see instead?
There was a panic about nil pointer dereference although that access can only happen if the length of slice is greater than zero.
The text was updated successfully, but these errors were encountered: