Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active August 29, 2015 14:24
Show Gist options
  • Save komuw/9c7fc2e070dd57908220 to your computer and use it in GitHub Desktop.
Save komuw/9c7fc2e070dd57908220 to your computer and use it in GitHub Desktop.
answer to a tour of go exercise on http handlers(web servers) https://tour.golang.org/methods/14
package main
import (
"fmt"
"net/http"
)
type Mystring string
type Mystruct struct {
Greeting string
Punct string
Who string
}
func (s Mystring) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s)
}
func (s Mystruct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s.Greeting, s.Punct, s.Who)
}
func main() {
var cool_str Mystring
cool_str = "I'm a frayed knot."
var cool_struct Mystruct
cool_struct = Mystruct{"Hello", ":", "Gophers!"}
// your http.Handle calls here
http.Handle("/string", cool_str)
http.Handle("/struct", cool_struct)
http.ListenAndServe("localhost:4000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment