Best Way to make own NotFound page using net/http ?

1,664 views
Skip to first unread message

RickyS

unread,
May 15, 2014, 5:22:01 PM5/15/14
to golan...@googlegroups.com
I find that net/http:server.go hardcodes in a particular NotFound handler and function.
I started to read the doc and server.go and found a Handle interface, several Handle functions, handle functions, a HandlerFunc and HandleFunc, and seven functions named ServeHTTP, but only the suggestion that for 'more control' I should create a custom Server.
But it seems to me that it is the ServeMux that selects the NotFound function, so I'd need my own mux.  I can see how to use my own mux, so maybe I can just use that in the default Server.
Is that the way to install a unique NotFoundHandler?   Are there any examples?

It seem like I'd have to re-implement a good chunk of server.go to do this, which I'd like to avoid.

  RickyS


Daniel Theophanes

unread,
May 15, 2014, 5:48:42 PM5/15/14
to golan...@googlegroups.com
You could use something like:

It has a NotFound handler (as well as a PanicHandler).

-Daniel

Robert

unread,
May 15, 2014, 6:24:56 PM5/15/14
to golan...@googlegroups.com
Wow, this httprouter package looks really well done. Cool stuff.

Matt Silverlock

unread,
May 15, 2014, 7:31:41 PM5/15/14
to golan...@googlegroups.com
And the ever-famous gorilla/mux: https://github.com/gorilla/mux

r.NotFoundHandler = http.HandlerFunc(notFound)

Goji (https://goji.io/) also has a similar feature:

Jesse McNelis

unread,
May 15, 2014, 8:31:05 PM5/15/14
to RickyS, golang-nuts


On 16/05/2014 7:22 am, "RickyS" <rickys...@gmail.com> wrote:
> Is that the way to install a unique NotFoundHandler?   Are there any examples?
>

You just have to write a http.HandlerFunc that will call http://golang.org/pkg/net/http/#ServeMux.Handler

If it returns a pattern of "" then it's not found and you can call your notfound handler.

eg. http://play.golang.org/p/WTdaAwrl12


Matt Harden

unread,
May 15, 2014, 8:37:25 PM5/15/14
to Jesse McNelis, RickyS, golang-nuts
I'm confused. Doesn't this example show how do do your own not found handler?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages