Last active
December 16, 2015 04:29
-
-
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.
This file contains 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" | |
"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