Skip to content

Instantly share code, notes, and snippets.

@cpliakas
Last active August 29, 2015 14:08
Show Gist options
  • Save cpliakas/f9b8ecec9fcd0a600fc3 to your computer and use it in GitHub Desktop.
Save cpliakas/f9b8ecec9fcd0a600fc3 to your computer and use it in GitHub Desktop.
Gorilla Mux
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", homeCallback).Methods("GET")
router.HandleFunc("/hello/{name:[a-z]+}", nameCallback).Methods("GET")
http.Handle("/", router)
log.Println("Listening on port 3000")
http.ListenAndServe(":3000", nil)
}
func homeCallback(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello world!"))
}
func nameCallback(res http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
name := params["name"]
res.Write([]byte("Hello " + name))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment