diff options
author | Levi Bostian <levi.bostian@gmail.com> | 2014-07-02 08:57:54 -0500 |
---|---|---|
committer | Levi Bostian <levi.bostian@gmail.com> | 2014-07-02 08:57:54 -0500 |
commit | 09459a25fe86965ddd625c99fce689c073f999b3 (patch) | |
tree | d16bfbd9df2c88acf2a6af336c6f2c4d24258de5 | |
parent | 5bd6cb3eb848c94ce31b4d1233f72bd576bb9cb7 (diff) | |
parent | f2e3d99476c0b549e468e3090d0938322e8142d4 (diff) |
Merge pull request #663 from vvo/patch-2
feat(webserver request): add a http.Get example
-rw-r--r-- | go.html.markdown | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/go.html.markdown b/go.html.markdown index 980fdc66..03227676 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -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 |