Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler.
Questions tagged [mux]
331 questions
73
votes
5 answers
Serving static content with a root URL with the Gorilla toolkit
I am attempting to use the Gorilla toolkit's mux package to route URLs in a Go web server. Using this question as a guide I have the following Go code:
func main() {
r := mux.NewRouter()
r.Handle("/",…

jason
- 1,247
- 1
- 9
- 25
39
votes
1 answer
How to set http.ResponseWriter Content-Type header globally for all API endpoints?
I am new to Go and I'm building a simple API with it now:
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"log"
"net/http"
)
func main() {
port := ":3000"
var…

Zulhilmi Zainudin
- 9,017
- 12
- 62
- 98
33
votes
3 answers
Gorilla mux optional query values
I've been working on a Go project where gorilla/mux is used as the router.
I need to be able to have query values associated with a route, but these values should be optional.
That means that I'd like to catch both /articles/123 and…

stassinari
- 543
- 2
- 5
- 8
21
votes
3 answers
Nesting subrouters in Gorilla Mux
I've been using gorilla/mux for my routing needs. But I noticed one problem, when I nest multiple Subrouters it doesn't work.
Here is the example:
func main() {
r := mux.NewRouter().StrictSlash(true)
api := r.Path("/api").Subrouter()
u…

Kortemy
- 691
- 1
- 5
- 8
19
votes
1 answer
access post parameters in handler
I can access GET parameters using mux:
import (
"github.com/gorilla/mux"
)
func main(){
rtr := mux.NewRouter()
rtr.HandleFunc("/logon", logonGet).Methods("GET")
}
func logonGet(w http.ResponseWriter, r *http.Request) {
params :=…

Maxim Yefremov
- 13,671
- 27
- 117
- 166
17
votes
1 answer
Should I use ServeMux or http directly in golang
I was wondering if I should create a new ServeMux and register it to the http.Server or should I invoke http.HandleFunc and http.Handler directly?
I think the route with a ServeMux is better because http.HandleFunc obviously messes with the global…

Matt3o12
- 4,192
- 6
- 32
- 47
17
votes
2 answers
Go using mux Router - How to pass my DB to my handlers
At the moment, I try to create a small Web-Project using Go for data handling on the server.
I try to pass my database-connection to my HandlerFunc(tions) but it does not work as expected. I am pretty new to golang, so maybe I did not understand…

Newbie
- 1,644
- 5
- 29
- 40
17
votes
2 answers
Go and Gorilla Mux NotFoundHandler not working
I just can't get this NotFoundHandler to work. I'd like to serve a static file on every get request, given that it exists, otherwise serve index.html. Here's my simplified router at the moment:
func fooHandler() http.Handler {
fn := func(w…

QlliOlli
- 637
- 3
- 13
- 25
14
votes
1 answer
When to use Golang's default MUX versus doing your own
I have seen a lot of posts talk about building your own MUX in Go, one of the many examples is here (http://thenewstack.io/building-a-web-server-in-go/).
When should you use the default versus defining your own? The Go docs and none of the blog…

jordan2175
- 878
- 2
- 10
- 20
11
votes
1 answer
Chisel: how to implement a one-hot mux that is efficient?
I have a table, where each row of the table contains state (registers). There is logic that chooses one particular row. Only one row receives the "selected" signal. State from that chosen row is then accessed. Either a portion of the state is…

seanhalle
- 973
- 7
- 27
11
votes
2 answers
What's wrong with my DMux 4 way?
It's seemingly close to working, it just is messing up at line 7 apparently?
/**
* 4-way demultiplexor.
* {a,b,c,d} = {in,0,0,0} if sel==00
* {0,in,0,0} if sel==01
* {0,0,in,0} if sel==10
* {0,0,0,in} if…

Doug Smith
- 29,668
- 57
- 204
- 388
10
votes
4 answers
Nested Gorilla Mux router does not work
Using code below, when I access /test2 it responds with 404 - not found. /test1 works correctly. Why is that? Is nesting not allowed despite the fact that routers implement http.Handler interface?
package main
import (
"fmt"
"net/http"
…

kars7e
- 786
- 1
- 6
- 19
10
votes
2 answers
GAE Golang Gorilla mux - 404 page not found
I've got some problems to use gorilla mux within GAE.
When I try it, I've '404 page not found'. The rootHandler function is not called ( no traces generated)
Below is part of my code, any ideas?
thk in advance
...
func init() {
r :=…

rlasjunies
- 319
- 1
- 5
- 11
9
votes
4 answers
golang mux, routing wildcard & custom func match
I'm using the mux package which seems to work quite well except that it doesn't seem to support complex routes or at least I don't get it how it does.
I have several routes as following:
router :=…

themihai
- 7,903
- 11
- 39
- 61
9
votes
3 answers
How to create a route with optional url var using gorilla mux?
I want to have an optional URL variable in route. I can't seem to find a way using mux package. Here's my current route:
func main() {
r := mux.NewRouter()
r.HandleFunc("/view/{id:[0-9]+}", MakeHandler(ViewHandler))
http.Handle("/", r)
…

anlogg
- 1,050
- 1
- 15
- 36