【已解决】go语言中想要获得Response的Cookies结果出错:cannot use resp.Cookies (type func() []*http.Cookie) as type []http.Cookie in assignment

【问题】

折腾:

【记录】go语言中处理http的cookie

期间,使用如下代码:

    var curCookies []http.Cookie = nil;
    curCookies = resp.Cookies

结果出错::

D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
# command-line-arguments
.\EmulateLoginBaidu.go:64: cannot use resp.Cookies (type func() []*http.Cookie) as type []http.Cookie in assignment

如图:

cannot use resp.Cookies (type func http.Cookie as type http.Cookie in assignment

即:

cannot use resp.Cookies (type func() []*http.Cookie) as type []http.Cookie in assignment

【解决过程】

1. 再去改为:

    var curCookies []http.Cookie = nil;
    curCookies = resp.Cookies()

结果是:

D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
# command-line-arguments
.\EmulateLoginBaidu.go:64: cannot use resp.Cookies() (type []*http.Cookie) as type []http.Cookie in assignment

2.再去改为:

    //var curCookies []http.Cookie = nil;
    //curCookies = resp.Cookies()
    var curCookies []*http.Cookie = nil;
    curCookies = resp.Cookies()

结果是:

可以了:

D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
# command-line-arguments
.\EmulateLoginBaidu.go:66: curCookies declared and not used

剩下的,只是如何继续去使用这些Cookie了。

 

【总结】

此处,想要获得Http的Response的Cookies,做法是:

mport  "net/http"

var curCookies []*http.Cookie = nil;
curCookies = resp.Cookies()

即可。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量