Skip to content

Instantly share code, notes, and snippets.

@oldergod
Created September 15, 2014 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldergod/39441f4b4db0e79630af to your computer and use it in GitHub Desktop.
Save oldergod/39441f4b4db0e79630af to your computer and use it in GitHub Desktop.
Tour of Go Http Handlers
package main
import (
"fmt"
"net/http"
)
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
func main() {
// your http.Handle calls here
http.Handle("/string", String("Let's go bitch"))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:4000", nil)
}
func (s String) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello! ", s)
}
func (s *Struct) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello! ", s.Greeting, s.Punct, s.Who)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment