A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/naoina/kocha-urlrouter below:

naoina/kocha-urlrouter: Better URL router collection for Go

Kocha-urlrouter

Better URL router collection for Go

Note: Kocha-urlrouter will be used as a sandbox for some implementations of a URL router. If you want a fast URL router, Please use Denco instead.

Interface:

go get -u github.com/naoina/kocha-urlrouter

Implementation:

go get -u github.com/naoina/kocha-urlrouter/doublearray

Kocha-urlrouter has multiple URL router implementations. See Implementations.

package main

import (
    "github.com/naoina/kocha-urlrouter"
    _ "github.com/naoina/kocha-urlrouter/doublearray"
)

type route struct {
    name string
}

func main() {
    router := urlrouter.NewURLRouter("doublearray")
    router.Build([]urlrouter.Record{
        urlrouter.NewRecord("/", &route{"root"}),
        urlrouter.NewRecord("/user/:id", &route{"user"}),
        urlrouter.NewRecord("/user/:name/:id", &route{"username"}),
        urlrouter.NewRecord("/static/*filepath", &route{"static"}),
    })

    router.Lookup("/")                    // returns *route{"root"}, nil slice.
    router.Lookup("/user/hoge")           // returns *route{"user"}, []urlrouter.Param{{"id", "hoge"}}
    router.Lookup("/user/hoge/7")           // returns *route{"username"}, []urlrouter.Param{{"name", "hoge"}, {"id", "7"}}
    router.Lookup("/static/path/to/file") // returns *route{"static"}, []urlrouter.Param{{"filepath", "path/to/file"}}
}

See Godoc for more docs.

cd $GOPATH/github.com/naoina/kocha-urlrouter
go test -bench . -benchmem ./...

Kocha-urlrouter is licensed under the MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4