mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:02:11 +08:00
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:
23
eBook/examples/chapter_15/http_fetch.go
Normal file
23
eBook/examples/chapter_15/http_fetch.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
30
eBook/examples/chapter_15/poll_url.go
Normal file
30
eBook/examples/chapter_15/poll_url.go
Normal 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
|
||||||
|
*/
|
42
eBook/examples/chapter_15/simple_webserver.go
Normal file
42
eBook/examples/chapter_15/simple_webserver.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
42
eBook/examples/chapter_15/twitter_status.go
Normal file
42
eBook/examples/chapter_15/twitter_status.go
Normal 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:
|
||||||
|
*/
|
Reference in New Issue
Block a user