Last active
August 29, 2015 14:18
-
-
Save evandertino/8f3bb67c4285cdef138b to your computer and use it in GitHub Desktop.
Go Exercise: HTTP Handlers - http://tour.golang.org/methods/14
This file contains hidden or 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" | |
| "log" | |
| "net/http" | |
| ) | |
| // type Hello implements http.Handler | |
| type Hello struct{} | |
| func (this Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintf(w, "Hello!") | |
| } | |
| type String string | |
| func (this String) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, this) | |
| } | |
| type Struct struct { | |
| Greeting string | |
| Punct string | |
| Who string | |
| } | |
| func (this Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, this.Greeting, this.Punct, this.Who) | |
| } | |
| func main() { | |
| http.Handle("/", Hello{}) | |
| http.Handle("/string", String("I'm a frayed knot.")) | |
| http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"}) | |
| if err := http.ListenAndServe("localhost:4000", nil); err != nil { | |
| log.Fatal(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment