Skip to content

Instantly share code, notes, and snippets.

@sighmin
Created April 14, 2015 06:25
Show Gist options
  • Save sighmin/854b1fa7e2adf7210d84 to your computer and use it in GitHub Desktop.
Save sighmin/854b1fa7e2adf7210d84 to your computer and use it in GitHub Desktop.
Go multiple http handlers example
package main
import (
"fmt"
"log"
"net/http"
)
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s)
}
func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s.Greeting, s.Punct, s.Who)
}
func main() {
http.Handle("/string", String("Hey i'm a string!"))
http.Handle("/struct", &Struct{"Hello", ",", "GOPHERS!"})
err := http.ListenAndServe("localhost:4000", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment