Categorygithub.com/naoina/kocha-urlrouter
modulepackage
0.0.0-20140609163054-ad3a6f079210
Repository: https://github.com/naoina/kocha-urlrouter.git
Documentation: pkg.go.dev

# README

Kocha-urlrouter Build Status

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.

Installation

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.

Usage

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.

Implementations

  • Double-Array github.com/naoina/kocha-urlrouter/doublearray
  • Regular-Expression github.com/naoina/kocha-urlrouter/regexp
  • Ternary Search Tree github.com/naoina/kocha-urlrouter/tst

Benchmark

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

License

Kocha-urlrouter is licensed under the MIT

# Packages

A URL router implemented by Double-Array Trie.
A URL router implemented by Regular-Expression.
No description provided by the author
A URL router implemented by Ternary Search Tree.

# Functions

isMetaChar returns whether the meta character.
NewRecord returns a new Record.
NewURLRouter returns the URLRouter with the specified name.
NextSeparator returns an index of next separator in path.
ParamNames returns parameter names in given path.
Register registers a Router with name.

# Constants

No description provided by the author
No description provided by the author

# Structs

param represents a name and value of path parameter.
Record represents a record data for a router construction.

# Interfaces

Router is an interface of factory of URLRouter.
URLRouter is an interface that must be implemented by a URL router.