Monday, September 23, 2013

Go (golang) and MongoDB Example 2 - A Series on Goku (MVC) Websites

Sorry I've been gone for so long. I decided to discontinue the Couchbase series in favor of MongoDB. I'm going to assume you have a working copy of Go and MongoDB. If you don't, you can get those at http://golang.org/ and http://www.mongodb.org/ . You will also need http://bazaar.canonical.com/en/ and Git (http://git-scm.com/ or https://github.com/) to install the necessary Go packages. You can retrieve those packages by issuing the following commands:

go get github.com/QLeelulu/goku

go get labix.org/v2/mgo

You will also need the /data/db path (C:\data\db in Windows) in your root for MongoDB to work.

This website/web server does not need IIS or Apache to run. It basically catalogs a CD collection. I used the LiteIDE (https://code.google.com/p/golangide/downloads/list) environment to write this program. Zeus (http://www.zeusedit.com/go.html) is another possibility, although you have to pay for it after 45 days.

Another piece of housekeeping is the directory tree. You should create a "gomgoweb" directory on your machine, and you'll need the following paths under it (use "\" instead of "/" if you're using Windows):

src/gomgoweb
src/gomgoweb/controllers
src/gomgoweb/models
src/gomgoweb/views/home
src/gomgoweb/views/band
src/gomgoweb/views/album
src/gomgoweb/views/shared
src/gomgoweb/static

If you have any experience with the MVC architecture, this tree should look familiar. Go expects to see all packages other than "main" under a src/<package_name> directory. The last thing you need to do (if they haven't been done for you) is set up the "gopath" environment variable. That should contain a path to your default Go package directory and the root path of this project. In Windows/DOS, that would look like

set gopath=C:\Go\pkg;C:\gomgoweb

Well, you get the idea. In the next installment, we'll get our hands dirty on some code.

No comments:

Post a Comment