Last active
December 8, 2018 04:39
-
-
Save sonyarianto/2e608ceea148e371f72f4bf5eca0f309 to your computer and use it in GitHub Desktop.
Web routing with httprouter mux
This file contains hidden or 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" | |
"github.com/julienschmidt/httprouter" | |
"log" | |
"net/http" | |
) | |
func HomeHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { | |
fmt.Fprint(w, "This is path / handled by httprouter!\n") | |
} | |
func NumberHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) { | |
number := params.ByName("number") | |
w.Header().Set("Content-Type", "text/html; charset=utf-8") | |
fmt.Fprint(w, "This is number <strong>"+number+"</strong>") | |
} | |
func main() { | |
router := httprouter.New() | |
router.GET("/", HomeHandler) | |
router.GET("/this/:number", NumberHandler) | |
log.Fatal(http.ListenAndServe(":3000", router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment