如何访问响应中的值

huangapple go评论117阅读模式
英文:

how to access values in response

问题

你好,我想知道在Go语言中是否有一种简单的方法来漂亮地打印一个结构体。

我正在尝试打印一个请求的头部信息。

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "io"
  6. "os"
  7. )
  8. func main() {
  9. resp, err := http.Get("http://google.com/")
  10. if err != nil {
  11. fmt.Println("ERROR")
  12. }
  13. defer resp.Body.Close()
  14. fmt.Println(resp)
  15. out, err := os.Create("filename.html")
  16. io.Copy(out, resp.Body)
  17. }

我得到了以下输出:

  1. &{200 OK 200 HTTP/1.1 1 1 map[Date:[Thu, 30 Jan 2014 09:05:33 GMT] Content-Type:[text/html; charset=ISO-8859-1] P3p:[CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."] X-Frame-Options:[SAMEORIGIN] Expires:[-1] Cache-Control:[private, max-age=0] Set-Cookie:[PREF=ID=5718798ffa48c7de:FF=0:TM=1391072733:LM=1391072733:S=NNfE1JSHH---lqDV; expires=Sat, 30-Jan-2016 09:05:33 GMT; path=/; domain=.google.com NID=67=aIMpDPrQ5-lBg0_1jFBSmg7KUZPprzZ6Srgbd_CVSK63Ugf_Jr75KwUaALOrBkdpAdFN6O9L8TQd2ng-g_o7HqIS-Drt_XHPj17KkjayHJ7xqUDAlL3ySyJafmRcMRD5; expires=Fri, 01-Aug-2014 09:05:33 GMT; path=/; domain=.google.com; HttpOnly] Server:[gws] X-Xss-Protection:[1; mode=block] Alternate-Protocol:[80:quic]] 0xc200092b80 -1 [chunked] false map[] 0xc20009a750}

不太明显这是什么类型的结构体,以及如何访问响应结构体中的各个值(我希望称之为结构体是正确的)。

英文:

Hello I was wondering if there was a easy way to pretty print a struct in golang

I'm trying to print out the header of a request

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. // "io/ioutil"
  6. "io"
  7. "os"
  8. )
  9. func main() {
  10. resp, err := http.Get("http://google.com/")
  11. if err != nil {
  12. fmt.Println("ERROR")
  13. }
  14. defer resp.Body.Close()
  15. fmt.Println(resp)
  16. // body, err := ioutil.ReadAll(resp.Body)
  17. out, err := os.Create("filename.html")
  18. io.Copy(out, resp.Body)
  19. }

and I get the following

  1. &{200 OK 200 HTTP/1.1 1 1 map[Date:[Thu, 30 Jan 2014 09:05:33 GMT] Content-Type:[text/html; charset=ISO-8859-1] P3p:[CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."] X-Frame-Options:[SAMEORIGIN] Expires:[-1] Cache-Control:[private, max-age=0] Set-Cookie:[PREF=ID=5718798ffa48c7de:FF=0:TM=1391072733:LM=1391072733:S=NNfE1JSHH---lqDV; expires=Sat, 30-Jan-2016 09:05:33 GMT; path=/; domain=.google.com NID=67=aIMpDPrQ5-lBg0_1jFBSmg7KUZPprzZ6Srgbd_CVSK63Ugf_Jr75KwUaALOrBkdpAdFN6O9L8TQd2ng-g_o7HqIS-Drt_XHPj17KkjayHJ7xqUDAlL3ySyJafmRcMRD5; expires=Fri, 01-Aug-2014 09:05:33 GMT; path=/; domain=.google.com; HttpOnly] Server:[gws] X-Xss-Protection:[1; mode=block] Alternate-Protocol:[80:quic]] 0xc200092b80 -1 [chunked] false map[] 0xc20009a750}

It wasn't readily apparent what kind of struct this was and how I could access the various values in the response struct (I hope its correct to call it a struct)

答案1

得分: 1

resp变量是一个结构体(你是对的 :))。你需要resp.Header<br>
resp.Header是一个具有字符串键和字符串值的映射。你可以很容易地打印它。<br><br>
例如:

  1. for header, value := range resp.Header {
  2. fmt.Println(header,value)
  3. }

有用的信息:

  1. 关于header
  2. 关于response
英文:

The resp variable is a struct (You right 如何访问响应中的值 ). You need resp.Header<br>
resp.Header is a map with string key and string value. You can easily print it.<br><br>
For example:

  1. for header, value := range resp.Header {
  2. fmt.Println(header,value)
  3. }

Useful things:

  1. About header
  2. About response

答案2

得分: 1

有几个漂亮的打印库可供选择。这是我非常喜欢的一个:

https://github.com/davecgh/go-spew

它允许你这样做:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. // "io/ioutil"
  6. "io"
  7. "os"
  8. "github.com/davecgh/go-spew"
  9. )
  10. func main() {
  11. resp, err := http.Get("http://google.com/")
  12. if err != nil {
  13. fmt.Println("ERROR")
  14. }
  15. defer resp.Body.Close()
  16. spew.Dump(resp)
  17. // body, err := ioutil.ReadAll(resp.Body)
  18. out, err := os.Create("filename.html")
  19. io.Copy(out, resp.Body)
  20. }

我认为这个库很适合你的需求。

英文:

There are a couple of pretty print libraries out there. This is the one I really prefer:

https://github.com/davecgh/go-spew

Allows you to do this:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. // &quot;io/ioutil&quot;
  6. &quot;io&quot;
  7. &quot;os&quot;
  8. &quot;github.com/davecgh/go-spew&quot;
  9. )
  10. func main() {
  11. resp, err := http.Get(&quot;http://google.com/&quot;)
  12. if err != nil {
  13. fmt.Println(&quot;ERROR&quot;)
  14. }
  15. defer resp.Body.Close()
  16. spew.Dump(resp)
  17. // body, err := ioutil.ReadAll(resp.Body)
  18. out, err := os.Create(&quot;filename.html&quot;)
  19. io.Copy(out, resp.Body)
  20. }

I think this will work out nicely for what you are asking.

huangapple
  • 本文由 发表于 2014年1月30日 17:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/21451945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定