如何获得价值作为回应

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)

The resp variable is a struct (You right :) ). You need resp.Header
resp.Header is a map with string key and string value. You can easily print it.

For example:

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

Useful things:

  1. About header
  2. About response

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.