Last active
August 29, 2015 14:24
-
-
Save komuw/9c7fc2e070dd57908220 to your computer and use it in GitHub Desktop.
answer to a tour of go exercise on http handlers(web servers) https://tour.golang.org/methods/14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
type Mystring string | |
type Mystruct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s Mystring) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, s) | |
} | |
func (s Mystruct) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, s.Greeting, s.Punct, s.Who) | |
} | |
func main() { | |
var cool_str Mystring | |
cool_str = "I'm a frayed knot." | |
var cool_struct Mystruct | |
cool_struct = Mystruct{"Hello", ":", "Gophers!"} | |
// your http.Handle calls here | |
http.Handle("/string", cool_str) | |
http.Handle("/struct", cool_struct) | |
http.ListenAndServe("localhost:4000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment