Documentation ¶
Overview ¶
Package router is a router for client-side web applications written in pure go which compiles to javascript via gopherjs (https://github.com/gopherjs/gopherjs).
Version 0.5.0 ¶
Router supports the following features:
- Write code in pure go. It feels like go, follows go idioms, and compiles with the go tools.
- Each route consists of a path and a handler function which is triggered when path matches.
- Routes can have parameters, which are passed through as an argument to handler functions.
- Router uses history.pushState and listens to the onpopstate event in browsers that support it. In older browsers it automatically falls back to using a hash.
- Router can be configured to automatically intercept link click events, triggering the appropriate route instead of requesting a new page.
For the full source code, a getting started guide, and more information visit https://github.com/go-humble/router.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶ added in v0.2.0
type Context struct { // Params is the parameters from the url as a map of names to values. Params map[string]string // Path is the path that triggered this particular route. If the hash // fallback is being used, the value of path does not include the '#' // symbol. Path string // InitialLoad is true iff this route was triggered during the initial // page load. I.e. it is true if this is the first path that the browser // was visiting when the javascript finished loading. InitialLoad bool // QueryParams is the query params from the URL. Because params may be // repeated with different values, the value part of the map is a slice QueryParams map[string][]string }
Context is used as an argument to Handlers
type Handler ¶
type Handler func(context *Context)
Handler is a function which is run in response to a specific route. A Handler takes a Context as an argument, which gives handler functions access to path parameters and other important information.
type Router ¶
type Router struct { // ShouldInterceptLinks tells the router whether or not to intercept click events // on links and call the Navigate method instead of the default behavior. // If it is set to true, the router will automatically intercept links when // Start, Navigate, or Back are called, or when the onpopstate event is triggered. ShouldInterceptLinks bool // ForceHashURL tells the router to use the hash component of the url to // represent different routes, even if history.pushState is supported. ForceHashURL bool // Verbose determines whether or not the router will log to console.log. // If true, the router will log a message if, e.g., a match cannot be found for // a particular path. Verbose bool // contains filtered or unexported fields }
Router is responsible for handling routes. If history.pushState is supported, it uses it to navigate from page to page and will listen to the "onpopstate" event. Otherwise, it sets the hash component of the url and listens to changes via the "onhashchange" event.
func (*Router) Back ¶
func (r *Router) Back()
Back will cause the browser to go back to the previous page. It has the same effect as the user pressing the back button, and is just a wrapper around history.back()
func (*Router) CanNavigate ¶ added in v0.3.0
CanNavigate returns true if the specified path can be navigated by the router, and false otherwise
func (*Router) HandleFunc ¶
HandleFunc will cause the router to call f whenever window.location.pathname (or window.location.hash, if history.pushState is not supported) matches path. path can contain any number of parameters which are denoted with curly brackets. So, for example, a path argument of "users/{id}" will be triggered when the user visits users/123 and will call the handler function with params["id"] = "123".
Example ¶
package main import ( "fmt" "github.com/go-humble/router" ) func main() { // Create a new Router object r := router.New() // Use HandleFunc to add routes. r.HandleFunc("/greet/{name}", func(context *router.Context) { // The handler for this route simply grabs the name parameter // from the map of params and says hello. fmt.Printf("Hello, %s\n", context.Params["name"]) }) // You must call Start in order to start listening for changes // in the url and trigger the appropriate handler function. r.Start() }
Output:
func (*Router) InterceptLinks ¶
func (r *Router) InterceptLinks()
InterceptLinks intercepts click events on links of the form <a href="/foo"></a> and calls router.Navigate("/foo") instead, which triggers the appropriate Handler instead of requesting a new page from the server. Since InterceptLinks works by setting event listeners in the DOM, you must call this function whenever the DOM is changed. Alternatively, you can set r.ShouldInterceptLinks to true, which will trigger this function whenever Start, Navigate, or Back are called, or when the onpopstate event is triggered. Even with r.ShouldInterceptLinks set to true, you may still need to call this function if you change the DOM manually without triggering a route.
func (*Router) Navigate ¶
Navigate will trigger the handler associated with the given path and update window.location accordingly. If the browser supports history.pushState, that will be used. Otherwise, Navigate will set the hash component of window.location to the given path.