Using mgo and writing handlers in Go with net/http

This is a short example of how to write handlers in Go and use the MongoDB mgo driver.

Assume you want to write handlers that are in a package and mount this handler under a certain prefix,
for instance a HelloHandler mounted on /hello/.

So you have a main package and a handler package, called hello.

Directory structure is

~/go/src/example/hello/
~/go/src/example/hello/main.go
~/go/src/example/hello/hello/
~/go/src/example/hello/hello/hello.go

Run it

and navigate to http://localhost:8080/hello/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.