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.
The mutexes are generally designed to work without any type of initialization. That is, a zero valued mutex is all you need to use them. To fix your program, declare foo
as a value. Not a pointer to a mutex:
var foo sync.RWMutex
func main() {
foo.Lock()
}
When you declare foo
as a pointer:
var foo *sync.RWMutex
foo
is a zero valued pointer (to type sync.RWMutex
), i.e. it does not point to a valid mutex value. foo
is nil
. That's why you get the nil pointer exception when calling a method.
However, note that to share the mutex between goroutines/functions, you're going to have to pass them as pointers, after they have been initialized (like above), or they'll not be shared.
From the docs:
An RWMutex must not be copied after first use.