Skip to content

Instantly share code, notes, and snippets.

@sonyarianto
Last active December 8, 2018 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonyarianto/2e608ceea148e371f72f4bf5eca0f309 to your computer and use it in GitHub Desktop.
Save sonyarianto/2e608ceea148e371f72f4bf5eca0f309 to your computer and use it in GitHub Desktop.
Web routing with httprouter mux
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