Empty Cookie Check

Hello all,

Is there any way to check if the cookie is set or not empty?

I am using the following code:

//Reading cookie
si, err := r.Cookie("mycookie")
CheckErr(err) //function to panic n log

//Checking if the value is there
if(len(si.Value)==0) {

}

But, it is giving me error:
runtime error: invalid memory address or nil pointer dereference

Am I doing anything wrong? Please help me on this.

The only two places in your code snippet that do a pointer dereference are r.Cookie() and si.Value. If err is nil, then si should not be nil, and thus si.Value should be a valid dereference.
Hence I would assume that the nil pointer dereference already happens at r.Cookie().

So the first thing I would check is if r can become nil at any point before the given code snippet.
I would add something like

fmt.Println(r)

before the call to r.Cookie() and also

fmt.Println(si, err)

after CheckErr(), to verify if all values are as expected.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.