[ANN] pat.go - A Sinatra style pattern muxer for Go's net/http library

671 views
Skip to first unread message

bmizerany

unread,
Jan 25, 2012, 6:50:46 PM1/25/12
to golan...@googlegroups.com
As the creator of Sinatra, I approve this:

This has been in use for awhile at Heroku. It's solid. Enjoy.

Jeff Hodges

unread,
Jan 25, 2012, 8:28:28 PM1/25/12
to golan...@googlegroups.com

Krzysztof Kowalik

unread,
Jan 25, 2012, 10:21:51 PM1/25/12
to golan...@googlegroups.com
2012/1/25 bmizerany <blake.m...@gmail.com>
As the creator of Sinatra, I approve this:

LOL ^^


Nice code btw, few mins ago i was looking at gorilla's mux and some other stuff to make the admin api of my webrocket a bit nicer. I think i will use this one :) Thx!

Cheers!
nu7

bmizerany

unread,
Jan 26, 2012, 12:31:18 AM1/26/12
to golan...@googlegroups.com
Ha. Yeah. Query() reparses and builds a new map on each invocation. Would rather just add to an existing map. Oh well.

Jeff Hodges

unread,
Jan 26, 2012, 12:50:13 AM1/26/12
to golan...@googlegroups.com
Word. I look forward to using this thing.

Rodrigo Moraes

unread,
Jan 26, 2012, 3:53:45 AM1/26/12
to golang-nuts
On Jan 25, 11:28 pm, Jeff Hodges wrote:
> That is a cute hack.https://github.com/bmizerany/pat.go/blob/master/mux.go#L19

Indeed. That trick also called my attention. :)

I wish Request had a map to store custom user variables. Too commonly
needed.

-- rodrigo

roger peppe

unread,
Jan 26, 2012, 4:36:58 AM1/26/12
to golan...@googlegroups.com

R. Rajesh Jeba Anbiah

unread,
Feb 13, 2012, 8:04:35 AM2/13/12
to golang-nuts
On Jan 26, 4:50 am, bmizerany <blake.mizer...@gmail.com> wrote:
> As the creator of Sinatra, I approve this: https://github.com/bmizerany/pat.go
>
> This has been in use for awhile at Heroku. It's solid. Enjoy.

I'm curious...
1. Which part of Heroku is using it?
2. *Now*, what would you personally prefer: pat.go or Sinatra? And
why?
3. (newbie question) Why are you calling it as "muxer"?

TIA

--
// PHP Ajax Cookbook -- http://link.packtpub.com/A50biZ //
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

R. Rajesh Jeba Anbiah

unread,
Feb 13, 2012, 8:07:15 AM2/13/12
to golang-nuts
On Jan 26, 1:53 pm, Rodrigo Moraes <rodrigo.mor...@gmail.com> wrote:
> On Jan 25, 11:28 pm, Jeff Hodges wrote:
>
> > That is a cute hack. https://github.com/bmizerany/pat.go/blob/master/mux.go#L19
>
> Indeed. That trick also called my attention. :)

(newbie question) Can't get the in-group appreciation. Can someone
please explain?

Paddy Foran

unread,
Feb 13, 2012, 1:42:34 PM2/13/12
to R. Rajesh Jeba Anbiah, golang-nuts
My guess it's referring to the practice of converting all the named URL parameters to a querystring and appending the query's actual querystring after it. This does mean that any named URL parameters can be overwritten by querystring parameters (I believe), and is essentially a workaround for there not being any idiomatic way to pass variables with a Request object in Go.

... I think.

Thanks,
Paddy Foran

Rodrigo Moraes

unread,
Feb 13, 2012, 6:12:15 PM2/13/12
to golang-nuts
On Feb 13, 4:42 pm, Paddy Foran wrote:
> This does mean that any named URL parameters can be overwritten
> by querystring parameters (I believe)

pat stores the parameters prefixed by a colon, which makes clashes a
lot more unlikely I think.

-- rodrigo

Paddy Foran

unread,
Feb 13, 2012, 6:35:48 PM2/13/12
to Rodrigo Moraes, golang-nuts
Didn't mean to imply accidental collisions--I think it would allow for some cool hacks, though I'm struggling to think of one.

Thanks,
Paddy Foran

R. Rajesh Jeba Anbiah

unread,
Feb 13, 2012, 11:42:57 PM2/13/12
to golang-nuts
On Feb 13, 11:42 pm, Paddy Foran <foran.pa...@gmail.com> wrote:
> My guess it's referring to the practice of converting all the named URL
> parameters to a querystring and appending the query's actual querystring
> after it. This does mean that any named URL parameters can be overwritten
> by querystring parameters (I believe), and is essentially a workaround for
> there not being any idiomatic way to pass variables with a Request object
> in Go.
>
> ... I think.

Thanks for the explanation.

Russ Cox

unread,
Feb 18, 2012, 7:42:27 PM2/18/12
to golan...@googlegroups.com
On Wed, Jan 25, 2012 at 18:50, bmizerany <blake.m...@gmail.com> wrote:
> https://github.com/bmizerany/pat.go

Thanks for sharing.
Your example on that page says:

func main() {
m := pat.New()
m.Get("/hello/:name", http.HandlerFunc(hello))
http.ListenAndServe("localhost:5000", m)
}

I'd like to encourage you to suggest instead:

func main() {
m := pat.New()
m.Get("/hello/:name", http.HandlerFunc(hello))
http.Handle("/", m)
http.ListenAndServe("localhost:5000", nil)
}

so that packages using the default mux can still register
for specific paths. As written, your example does not
allow easy use of the net/http/pprof import to enable
profiling at /debug/pprof/*, while using http.Handle and
the nil argument to ListenAndServe does.

Russ

bmizerany

unread,
Mar 31, 2012, 6:08:26 PM3/31/12
to golan...@googlegroups.com, r...@golang.org
I just got this. Will do.
Reply all
Reply to author
Forward
0 new messages