The following code crashes with the following error, why??
package main
import (
"sync"
)
var foo *sync.RWMutex
func main() {
foo.Lock()
}
outputs:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0x8125c]
goroutine 1 [running]:
sync.(*RWMutex).Lock(0x0, 0x104000f0)
/usr/local/go/src/sync/rwmutex.go:86 +0x1c
main.main()
/tmp/sandbox093456788/main.go:11 +0x20
Program exited.
it will not happen, when you use one of the following lines
var foo = new(sync.RWMutex)
var foo sync.RWMutex
for the initialization of the foo variable.