最新消息:VPS服务器又从fzhost.net换回linode了,主题仍用朋友推荐的大前端D8

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

GO crifan 93浏览 0评论

【问题】

折腾:

【记录】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()

即可。

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
15 queries in 0.185 seconds, using 9.71MB memory