This occurs, as the error message says, when you try to dereference (use) nil
. Without knowing which line the error was reported on, I would have to guess it’s here:
cd, _ := r.Cookie("hello")
if cd.Value == "username" { // <-- This line
// do ...
}
Because you’re discarding the error from r.Cookie()
instead of checking the error, so you don’t know that the cookie hello
doesn’t exist, meaning cd
is nil
. So when you try to use cd.Value
, you’re dereferencing a nil pointer, causing the panic you’re seeing.
solved Golang error “invalid memory address or nil pointer dereference”, Why this happens? [duplicate]