Gorilla is a web toolkit for the Go programming language. It provides several modules to aid web programming: a URL router and dispatcher, a consistent session management system, and a http.response-struct mapper. It is licensed under the New BSD License.
Questions tagged [gorilla]
590 questions
77
votes
1 answer
How do I pass arguments to my handler
I am trying to pass my database object along to my handlers, instead of having a global object. But I don't know if this is possible, I'm using Gorilla Mux package, and I can see that it takes a closure as a second param.
//…

MartinElvar
- 5,695
- 6
- 39
- 56
52
votes
5 answers
Unit testing for functions that use gorilla/mux URL parameters
TLDR: gorilla/mux used to not offer the possibility to set URL Vars. Now it does, that's why the second-most upvoted answer was the right answer for a long time.
Original question to follow:
Here's what I'm trying to do :
main.go
package…

Sebastian-Laurenţiu Plesciuc
- 2,413
- 1
- 24
- 41
49
votes
8 answers
Making golang Gorilla CORS handler work
I have fairly simple setup here as described in the code below. But I am not able to get the CORS to work. I keep getting this error:
XMLHttpRequest cannot load http://localhost:3000/signup. Response to
preflight request doesn't pass access…

Moon
- 33,439
- 20
- 81
- 132
47
votes
5 answers
How to add Authorization Header to Angular http request?
This is my first post.
I've just started learning Go and Angular and I'm attempting to connect the angular app to a go api. I've written both and am stuck identifying the root of the problem. I thought it was a CORS problem, but it works fine if I…

gobuckeyes2
- 495
- 1
- 4
- 7
40
votes
2 answers
Golang Gorilla mux with http.FileServer returning 404
The problem I'm seeing is that I'm trying to use the http.FileServer with the Gorilla mux Router.Handle function.
This doesn't work (the image returns a 404)..
myRouter := mux.NewRouter()
myRouter.Handle("/images/", http.StripPrefix("/images/",…

dodgy_coder
- 12,407
- 10
- 54
- 67
38
votes
5 answers
Gorilla mux custom middleware
I am using gorilla mux for manage routing. What I am missing is to integrate a middleware between every request.
For example
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
func…

softshipper
- 32,463
- 51
- 192
- 400
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
2 answers
Rest APIs in Go - using net/http vs. a library like Gorilla
I see that Go itself has a package net/http, which is adequate at providing everything you need to get your own REST APIs up and running. However, there are a variety of frameworks; the most popular maybe say gorilla.
Considering that one of the…

Ankur Garg
- 2,553
- 8
- 30
- 41
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
20
votes
2 answers
Retrieve optional query variables with gorilla mux?
I'm writing a handler that can take either POST or GET. As such, I want the option of being able to say:
http://host/query?parm1=value&parm2=value
I was assuming that Gorilla mux would then give me:
{
"parm1": "value",
"parm2":…

Scott Deerwester
- 3,503
- 4
- 33
- 56
20
votes
4 answers
Can't use go tool pprof with an existing server
I have an existing http server which I would like to profile. I have included _ "net/http/pprof"to my imports, and I already have http server running:
router := createRouter()
server := &http.Server {
Addr: ":8080",
Handler: …

Max Malysh
- 29,384
- 19
- 111
- 115
19
votes
4 answers
how to organize gorilla mux routes?
i am using Gorilla Mux for writing a REST API and i am having trouble organizing my routes, currently all of my routes are defined in the main.go file like this
//main.go
package main
import (
"NovAPI/routes"
"fmt"
…

zola
- 5,737
- 8
- 33
- 48
17
votes
3 answers
Golang sharing configurations between packages
So I just started learning the Go programming language and have spent hours on end looking at examples, references and so forth. As most of you would agree there is no better way to learn a language than to dive in and make something, which is what…

MrSSS16
- 449
- 3
- 7
- 21
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
2 answers
I need to connect to an existing websocket server using go lang
Theres a websocket running in my localhost on ws://localhost:8080/ws
I need go lang code that can create a websocket client and connect to this server.
My Google-Fu skills failed to teach me a simple way to do this.
Thank you.

SudoPlz
- 20,996
- 12
- 82
- 123