如何从 Golang 中的 response.Cookies() 按名称获取单个 cookie?

How to get a single cookie by name from response.Cookies() in Golang?

有没有办法从 response.Cookies() 中按名称只获取一个 cookie?

假设我需要下面这个饼干罐中的 wr_entry_path 饼干。 [wr_entry_path=/aP3Mk1i6M/xcp0g1/vMg/Qpr7ccN0OE3p/YxU3A31SAw/RWoGdE/k2DyQ; Path=/; Expires=Tue, 19 Apr 2022 19:40:03 GMT waitingroom=1650392103~id=072e61d9e7fa58639a6a2af28cea89de; Path=/; HttpOnly; Secure; SameSite=None]

感谢任何帮助!!

Response.Cookies() returns you a slice of all parsed http.Cookie秒。只需使用一个循环遍历它并找到具有您要查找的名称的那个:

cookies := resp.Cookies()
for _, c := range cookies {
    if c.Name == "wr_entry_path" {
        // Found! Use it!
        fmt.Println(c.Value) // The cookie's value
    }
}