Zeus

Zeus is a super-duper, simple and fast HTTP router for Go, nothing more, nothing less.

Install

go get github.com/daryl/zeus

Usage

package main

import (
    "fmt"
    "github.com/daryl/zeus"
    "net/http"
)

func main() {
    mux := zeus.New()
    // Supports named parameters.
    mux.GET("/users/:id", showUser)
    // Supports wildcards anywhere.
    mux.GET("/foo/*", catchFoo)
    // Custom 404 handler.
    mux.NotFound = notFound
    // Listen and serve.
    mux.Listen(":4545")
}

func showUser(w http.ResponseWriter, r *http.Request) {
    var id string

    // Get a map of all
    // route variables.
    vm := zeus.Vars(r)

    id = vm["id"]

    // Or just one.
    id = zeus.Var(r, "id")

    fmt.Fprintf(w, "User ID: %s", id)
}

func catchFoo(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Gotta catch 'em all"))
}

func notFound(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Nothing to see here"))
}

Documentation

For further documentation, check out GoDoc.



Zeus

Zeus是Go的超级复制,简单快速的HTTP路由器,没有什么比较少。

Install

go get github.com/daryl/zeus

Usage

package main

import ( "fmt" "github.com/daryl/zeus" "net/http" )

func main() { mux := zeus.New() // Supports named parameters. mux.GET("/users/:id", showUser) // Supports wildcards anywhere. mux.GET("/foo/*", catchFoo) // Custom 404 handler. mux.NotFound = notFound // Listen and serve. mux.Listen(":4545") }

func showUser(w http.ResponseWriter, r *http.Request) { var id string

<span class="pl-c"><span class="pl-c">//</span> Get a map of all</span>
<span class="pl-c"><span class="pl-c">//</span> route variables.</span>
<span class="pl-smi">vm</span> <span class="pl-k">:=</span> zeus.<span class="pl-c1">Vars</span>(r)

id = vm[<span class="pl-s"><span class="pl-pds">&#34;</span>id<span class="pl-pds">&#34;</span></span>]

<span class="pl-c"><span class="pl-c">//</span> Or just one.</span>
id = zeus.<span class="pl-c1">Var</span>(r, <span class="pl-s"><span class="pl-pds">&#34;</span>id<span class="pl-pds">&#34;</span></span>)

fmt.<span class="pl-c1">Fprintf</span>(w, <span class="pl-s"><span class="pl-pds">&#34;</span>User ID: <span class="pl-c1">%s</span><span class="pl-pds">&#34;</span></span>, id)

}

func catchFoo(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Gotta catch 'em all")) }

func notFound(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Nothing to see here")) }

Documentation

有关进一步的文档,请查看 GoDoc




相关问题推荐