add missing code example of chapter 15.3 (#678)

example code from:
https://sites.google.com/site/thewaytogo2012/Downhome/Topic3/code_examples.zip
This commit is contained in:
marjune
2019-07-16 10:57:06 +08:00
committed by ᴊ. ᴄʜᴇɴ
parent 9865ae64a9
commit 00e4346204
4 changed files with 137 additions and 0 deletions

View File

@@ -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)
}
}

View File

@@ -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
*/

View File

@@ -0,0 +1,42 @@
// simple_webserver.go
package main
import (
"net/http"
"io"
)
const form = `<html><body><form action="#" method="post" name="bar">
<input type="text" name="in"/>
<input type="submit" value="Submit"/>
</form></html></body>`
/* handle a simple get request */
func SimpleServer(w http.ResponseWriter, request *http.Request) {
io.WriteString(w, "<h1>hello, world</h1>")
}
/* 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)
}
}

View File

@@ -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<exit code="0" msg="process exited normally"/>
After Go1: no output: name: { user} status:
*/