Last active
August 29, 2015 14:08
-
-
Save cpliakas/f9b8ecec9fcd0a600fc3 to your computer and use it in GitHub Desktop.
Gorilla Mux
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 ( | |
"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