Skip to content

Instantly share code, notes, and snippets.

@royling
Last active December 16, 2015 04:29
Show Gist options
  • Save royling/bbd7d1e4786b5f90d331 to your computer and use it in GitHub Desktop.
Save royling/bbd7d1e4786b5f90d331 to your computer and use it in GitHub Desktop.
Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server.
package main
import (
"fmt"
"net/http"
)
type MyString string
// implements http.Handler
func (s MyString) ServeHTTP(
writer http.ResponseWriter,
req *http.Request) {
fmt.Fprint(writer, s)
}
type MyStruct struct {
Greeting string
Punct string
Who string
}
// implements http.Handler
func (s *MyStruct) ServeHTTP(
writer http.ResponseWriter,
req *http.Request) {
fmt.Fprint(writer, s.Greeting, s.Punct, s.Who)
}
func main() {
// register http handlers with respective path/pattern
http.Handle("/string", MyString("I'm a frayed knot."))
http.Handle("/struct", &MyStruct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:4000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment