I recently found some go code was running in a test framework where there were some uninitialized variables.
This lead to a panic, which had a stack trace containing some c code at the top.
Is there a way, in functions, that we can concisely detect wether the struct which is referenced as the implementing member is nil? i.e.
func ( d *Dog ) canBark Bool {
//if d is nil, cryptic exception is thrown.
//is there a way to defend against this?
}
The error which is thrown is
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4ec83a]
goroutine 16 [running]:
runtime.panic(0x9b7400, 0xf7ddf3)
It seems that in go, such lower level errors should occur very rarely, maybe never at all...
There might be a Golang way to deal with nil references which don't clutter the code too much with if/else logic. For example, in java, you can wrap a large code segment in a null pointer exception handler.