diff --git a/eBook/examples/chapter_15/http_fetch.go b/eBook/examples/chapter_15/http_fetch.go new file mode 100644 index 0000000..52ae89e --- /dev/null +++ b/eBook/examples/chapter_15/http_fetch.go @@ -0,0 +1,23 @@ +// httpfetch.go +package main + +import ( + "fmt" + "net/http" + "io/ioutil" + "log" +) + +func main() { + res, err := http.Get("http://www.google.com") + CheckError(err) + data, err := ioutil.ReadAll(res.Body) + CheckError(err) + fmt.Printf("Got: %q", string(data)) +} + +func CheckError(err error) { + if err != nil { + log.Fatalf("Get: %v", err) + } +} diff --git a/eBook/examples/chapter_15/poll_url.go b/eBook/examples/chapter_15/poll_url.go new file mode 100644 index 0000000..f59119f --- /dev/null +++ b/eBook/examples/chapter_15/poll_url.go @@ -0,0 +1,30 @@ +// poll_url.go +package main + +import ( + "fmt" + "net/http" +) + +var urls = []string{ + "http://www.google.com/", + "http://golang.org/", + "http://blog.golang.org/", +} + +func main() { + // Execute an HTTP HEAD request for all url's + // and returns the HTTP status string or an error string. + for _, url := range urls { + resp, err := http.Head(url) + if err != nil { + fmt.Println("Error", url, err) + } + fmt.Print(url, ": ", resp.Status) + } +} +/* Output: +http://www.google.com/ : 302 Found +http://golang.org/ : 200 OK +http://blog.golang.org/ : 200 OK +*/ diff --git a/eBook/examples/chapter_15/simple_webserver.go b/eBook/examples/chapter_15/simple_webserver.go new file mode 100644 index 0000000..a8634f6 --- /dev/null +++ b/eBook/examples/chapter_15/simple_webserver.go @@ -0,0 +1,42 @@ +// simple_webserver.go +package main + +import ( + "net/http" + "io" +) + +const form = `
+ + +
` + +/* handle a simple get request */ +func SimpleServer(w http.ResponseWriter, request *http.Request) { + io.WriteString(w, "

hello, world

") +} + +/* handle a form, both the GET which displays the form + and the POST which processes it.*/ +func FormServer(w http.ResponseWriter, request *http.Request) { + w.Header().Set("Content-Type", "text/html") + switch request.Method { + case "GET": + /* display the form to the user */ + io.WriteString(w, form ); + case "POST": + /* handle the form data, note that ParseForm must + be called before we can extract form data*/ + //request.ParseForm(); + //io.WriteString(w, request.Form["in"][0]) + io.WriteString(w, request.FormValue("in")) + } +} + +func main() { + http.HandleFunc("/test1", SimpleServer) + http.HandleFunc("/test2", FormServer) + if err := http.ListenAndServe(":8088", nil); err != nil { + panic(err) + } +} diff --git a/eBook/examples/chapter_15/twitter_status.go b/eBook/examples/chapter_15/twitter_status.go new file mode 100644 index 0000000..5d1eb27 --- /dev/null +++ b/eBook/examples/chapter_15/twitter_status.go @@ -0,0 +1,42 @@ +// twitter_status.go +package main + +import ( + "net/http" + "fmt" + "encoding/xml" + "io/ioutil" +) +/* these structs will house the unmarshalled response. + they should be hierarchically shaped like the XML + but can omit irrelevant data. */ +type Status struct { + Text string +} + +type User struct { + XMLName xml.Name + Status Status +} +// var user User + +func main() { + // perform an HTTP request for the twitter status of user: Googland + resp, _ := http.Get("http://twitter.com/users/Googland.xml") + // initialize the structure of the XML response + user := User{xml.Name{"", "user"}, Status{""}} + // unmarshal the XML into our structures + defer resp.Body.Close() + if body, err := ioutil.ReadAll(resp.Body); err != nil { + fmt.Printf("error: %s", err.Error()) + } else { + fmt.Printf("%s ---", body) + xml.Unmarshal(body, &user) + } + fmt.Printf("name: %s ", user.XMLName) + fmt.Printf("status: %s", user.Status.Text) +} +/* Output: +status: Robot cars invade California, on orders from Google: Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p +After Go1: no output: name: { user} status: +*/ \ No newline at end of file