Basic example
gorouter supports following http implementations:
package main
import (
"fmt"
"log"
"net/http"
"github.com/vardius/gorouter/v4"
"github.com/vardius/gorouter/v4/context"
)
func index(w http.ResponseWriter, _ *http.Request) {
if _, err := fmt.Fprint(w, "Welcome!\n"); err != nil {
panic(err)
}
}
func hello(w http.ResponseWriter, r *http.Request) {
params, _ := context.Parameters(r.Context())
if _, err := fmt.Fprintf(w, "hello, %s!\n", params.Value("name")); err != nil {
panic(err)
}
}
func main() {
router := gorouter.New()
router.GET("/", http.HandlerFunc(index))
router.GET("/hello/{name}", http.HandlerFunc(hello))
log.Fatal(http.ListenAndServe(":8080", router))
}
package main
import (
"fmt"
"log"
"github.com/valyala/fasthttp"
"github.com/vardius/gorouter/v4"
"github.com/vardius/gorouter/v4/context"
)
func index(_ *fasthttp.RequestCtx) {
fmt.Print("Welcome!\n")
}
func hello(ctx *fasthttp.RequestCtx) {
params := ctx.UserValue("params").(context.Params)
fmt.Printf("Hello, %s!\n", params.Value("name"))
}
func main() {
router := gorouter.NewFastHTTPRouter()
router.GET("/", index)
router.GET("/hello/{name}", hello)
log.Fatal(fasthttp.ListenAndServe(":8080", router.HandleFastHTTP))
}