Hack Forums
[Help] Go Language - panic: runtime error: invalid memory address or nil pointer dereference - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Help] Go Language - panic: runtime error: invalid memory address or nil pointer dereference (/showthread.php?tid=6182083)



Go Language - panic: runtime error: invalid memory address or nil pointer dereference - Lenovo - Nov 22, 2021

When I run my program written that was written in Go the following error occurs:

Code
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x605acf]

goroutine 19 [running]:
net/http/cookiejar.(*Jar).cookies(0x0, 0xc0001062d0, {0x20, 0x0, 0x8872e0})
        /usr/local/go/src/net/http/cookiejar/jar.go:168 +0x14f
net/http/cookiejar.(*Jar).Cookies(0x68f71d, 0x5aea9e)
        /usr/local/go/src/net/http/cookiejar/jar.go:156 +0x3d
building-detroit-bot/req.updateCookies(0x7f2504670ed8, 0xc000106240)
        /root/safe/tools/team-seas/req/post.go:41 +0x65
building-detroit-bot/req.MakePost(0xc000108000, {0xc0000e4100, 0x72}, {0xc0000cc078, 0x17}, 0xc0000adbf0, 0x0)
        /root/safe/tools/team-seas/req/post.go:35 +0x50e
building-detroit-bot/api/ids.GetIDs({0xc0000e4100, 0x72}, {0xc0000cc078, 0x17})
        /root/safe/tools/team-seas/api/ids/get.go:33 +0x185
main.work(0x0, 0x5, 0x1)
        /root/safe/tools/team-seas/main.go:45 +0x1d8
created by main.main
        /root/safe/tools/team-seas/main.go:89 +0x337

The updateCookies function looks like this:

Code
func updateCookies(jar *cookiejar.Jar, res *http.Response) {
        u, _ := url.Parse(endpoints.TokensURL)
        cookies := jar.Cookies(u)
        for _, cookie := range res.Cookies() {
                c := &http.Cookie{
                        Name:   cookie.Name,
                        Value:  cookie.Value,
                        Path:   cookie.Path,
                        Domain: cookie.Domain,
                }
                cookies = append(cookies, c)
                fmt.Println("Cookie:", cookie.Name, "=", cookie.Value)
        }
        jar.SetCookies(u, cookies)
}

I am new to Go and need to fix this error, what is a solution for this problem?