Skip to content

Commit

Permalink
Merge pull request adambard#663 from vvo/patch-2
Browse files Browse the repository at this point in the history
feat(webserver request): add a http.Get example
  • Loading branch information
levibostian committed Jul 2, 2014
2 parents 5bd6cb3 + f2e3d99 commit 09459a2
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions go.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,31 @@ func learnConcurrency() {

// A single function from package http starts a web server.
func learnWebProgramming() {

// First parameter of ListenAndServe is TCP address to listen to.
// Second parameter is an interface, specifically http.Handler.
err := http.ListenAndServe(":8080", pair{})
fmt.Println(err) // don't ignore errors
go func() {
err := http.ListenAndServe(":8080", pair{})
fmt.Println(err) // don't ignore errors
}()

requestServer();
}


// Make pair an http.Handler by implementing its only method, ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Serve data with a method of http.ResponseWriter.
w.Write([]byte("You learned Go in Y minutes!"))
}

func requestServer() {
resp, err := http.Get("http://localhost:8080")
fmt.Println(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("\nWebserver said: `%s`", string(body))
}
```

## Further Reading
Expand Down

0 comments on commit 09459a2

Please sign in to comment.