英文:
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)
}
有用的信息:
英文:
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:
答案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 (
"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)
}
I think this will work out nicely for what you are asking.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论