I've created a simple go program (basically just example code):
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func getPage(url string) (body []byte, err error) {
resp, err := http.Get(url)
body = nil
if (err != nil) {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
return
}
func main() {
startUrl := "http://slashdot.org/"
body, err := getPage(startUrl)
if (err != nil) {
fmt.Println("Error: " , err)
}
fmt.Println(body)
}
I'm trying to go through the docs to understand how it all fits together.
First issue: http.Get(). It's not in the docs (at http://golang.org/pkg/net/http/). Except it is, but under Response. However there are 2 other Get() functions. How do I know that net/http.Get is actually the Get() on the Response type?
Anyway, so http.Get() returns a Response with a Body
of io.ReadCloser
. ioutil.ReadAll() takes an io.Reader - but how can I find other functions that accept this as a parameter? It kind of seems like the docs are 'backwards' - if I know which function I want I can find docs, but if I have a type, how can I find what functions will work with it?