Skip to content

Instantly share code, notes, and snippets.

@evandertino
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save evandertino/8f3bb67c4285cdef138b to your computer and use it in GitHub Desktop.

Select an option

Save evandertino/8f3bb67c4285cdef138b to your computer and use it in GitHub Desktop.
Go Exercise: HTTP Handlers - http://tour.golang.org/methods/14
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