Frage

I recently started learning Go and really like it so far. I would like to learn how to make a REST API with the mgo Mongo driver.

At the website, there are three APIs: mgo, mgo/bson, mgo/txn. What do they mean?

War es hilfreich?

Lösung

Darshan is right: there are good references for those details at the project website. Specifically, you can find links to the package APIs of mgo, mgo/bson, and mgo/txn

Just providing a quick overview of each of the packages, since this seems missing from the web page (I should fix that):

labix.org/v2/mgo

This is the MongoDB driver itself. If you want to talk to a MongoDB database, this is the package to start with. Have a look at the mgo.Dial function, and the example in the project website.

labix.org/v2/mgo/bson

This package implements the marshaling and unmarshaling of BSON documents, following the BSON specification. It doesn't depend on any of the other two packages, and may be used by itself when one wants to simply serialize/deserialize documents in that format for whatever reason.

The mgo package uses it to implement all marshaling and unmarshaling functionality, so the details you find in the documentation of the mgo/bson package in terms of field tags, etc, are all valid when working with the mgo package as well.

labix.org/v2/mgo/txn

This package implements the mgo-specific multi-document transaction support for MongoDB. It implements its functionality on top of the mgo package, and neither of the other two packages depends on it. If you're just getting started with your MongoDB use, you most probably don't need this package.

Andere Tipps

They aren't three separate APIs; it's one API that's modularized. mgo is the core package. mgo/bson is the BSON implementation. mgo/txn supports multi-document transactions.

You'd start by just using mgo and add either of the subpackages if you need the features they provide. There's example code at the mgo homepage that demonstrates usage.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top