diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-02-29 11:23:13 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-02-29 11:23:13 +0100 |
commit | b6d7097a0d6072199f2cd74d67404890697cf78a (patch) | |
tree | a2193c528a79fd5606507568859ee5067c6b86e4 /src/pkg/net/http/example_test.go | |
parent | 4cecda6c347bd6902b960c6a35a967add7070b0d (diff) | |
download | golang-b6d7097a0d6072199f2cd74d67404890697cf78a.tar.gz |
Imported Upstream version 2012.02.22upstream-weekly/2012.02.22
Diffstat (limited to 'src/pkg/net/http/example_test.go')
-rw-r--r-- | src/pkg/net/http/example_test.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/pkg/net/http/example_test.go b/src/pkg/net/http/example_test.go new file mode 100644 index 000000000..2584afc43 --- /dev/null +++ b/src/pkg/net/http/example_test.go @@ -0,0 +1,51 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package http_test + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" +) + +func ExampleHijacker() { + http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) { + hj, ok := w.(http.Hijacker) + if !ok { + http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) + return + } + conn, bufrw, err := hj.Hijack() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // Don't forget to close the connection: + defer conn.Close() + bufrw.WriteString("Now we're speaking raw TCP. Say hi: ") + bufrw.Flush() + s, err := bufrw.ReadString('\n') + if err != nil { + log.Printf("error reading string: %v", err) + return + } + fmt.Fprintf(bufrw, "You said: %q\nBye.\n", s) + bufrw.Flush() + }) +} + +func ExampleGet() { + res, err := http.Get("http://www.google.com/robots.txt") + if err != nil { + log.Fatal(err) + } + robots, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + res.Body.Close() + fmt.Printf("%s", robots) +} |