如何访问响应中的值

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

how to access values in response

问题

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

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

package main

import (
	"fmt"
	"net/http"
	"io"
	"os"
)

func main() {

	resp, err := http.Get("http://google.com/")
	if err != nil {
		fmt.Println("ERROR")
	}
	defer resp.Body.Close()
	fmt.Println(resp)
	out, err := os.Create("filename.html")
	io.Copy(out, resp.Body)

}

我得到了以下输出:

&{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

package main

import (
	"fmt"
	"net/http"
	// "io/ioutil"
	"io"
	"os"
)

func main() {

	resp, err := http.Get("http://google.com/")
	if err != nil {
		fmt.Println("ERROR")
	}
	defer resp.Body.Close()
	fmt.Println(resp)
	// body, err := ioutil.ReadAll(resp.Body)
	out, err := os.Create("filename.html")
	io.Copy(out, resp.Body)

}

and I get the following

&{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>
例如:

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

有用的信息:

  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:

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

Useful things:

  1. About header
  2. About response

答案2

得分: 1

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

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

它允许你这样做:

package main

import (
    "fmt"
    "net/http"
    // "io/ioutil"
    "io"
    "os"
    "github.com/davecgh/go-spew"
)

func main() {

    resp, err := http.Get("http://google.com/")
    if err != nil {
        fmt.Println("ERROR")
    }
    defer resp.Body.Close()
    spew.Dump(resp)
    // body, err := ioutil.ReadAll(resp.Body)
    out, err := os.Create("filename.html")
    io.Copy(out, resp.Body)

}

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

英文:

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:

package main

import (
    &quot;fmt&quot;
    &quot;net/http&quot;
    // &quot;io/ioutil&quot;
    &quot;io&quot;
    &quot;os&quot;
    &quot;github.com/davecgh/go-spew&quot;
)

func main() {

    resp, err := http.Get(&quot;http://google.com/&quot;)
    if err != nil {
        fmt.Println(&quot;ERROR&quot;)
    }
    defer resp.Body.Close()
    spew.Dump(resp)
    // body, err := ioutil.ReadAll(resp.Body)
    out, err := os.Create(&quot;filename.html&quot;)
    io.Copy(out, resp.Body)

}

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:

确定