gorouter

gorouter

  • Docs
  • Help
  • GitHub

›Quick Start

Quick Start

  • Installation
  • Basic example

Router

  • Routing
  • Middleware
  • Mounting Sub-Router

Examples

    Authentication

    • Basic Authentication

    Serving Files

    • Static Files
  • HTTPS
  • HTTP2
  • Multidomain
  • Panic Recovery
  • App Handler

Benchmark

  • Benchmark

Basic example

gorouter supports following http implementations:

  • net/http
  • fasthttp
net/http
valyala/fasthttp
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))
}
← InstallationRouting →
gorouter
Docs
DocumentationGoDoc
Community
Support
More
rafallorenz.comGitHubStar
Copyright © 2024 Rafał Lorenz