Using NoSQL with MongoDB and mgo
You might first think that Go is better suited to relational databases due to Go structs and because Go is a typed language. When working with something like the mgo
package, Go can nearly arbitrarily store and retrieve struct objects. If you version your objects, your schema can adapt and it can provide a very flexible development environment.
Some libraries do a better job of hiding or elevating these abstractions. The mgo
package is an excellent example of a library that does an excellent job of the former. This recipe will create a connection in a similar way to Redis and MySQL, but will store and retrieve an object without even defining a concrete schema.
Getting ready
Configure your environment according to these steps:
- Download and install Go on your operating system from https://golang.org/doc/install, and configure your
GOPATH
environment variable. - Open a terminal/console application.
- Navigate to your
GOPATH/src
and create a project directory such as...