Go 博客

Spotlight on external Go libraries

2011/06/03

While the Go authors have been working hard at improving Go's standard library, the greater community has created a growing ecosystem of external libraries. In this post we look at some popular Go libraries and how they can be used.

Mgo (pronounced "mango") is a MongoDB database driver. MongoDB is a document-oriented database with a long list of features suitable for a broad range of uses. The mgo package provides a rich, idiomatic Go API for working with MongoDB, from basic operations such as inserting and updating records to the more advanced MapReduce and GridFS features. Mgo has a bunch of cool features including automated cluster discovery and result pre-fetching - see the mgo homepage for details and example code. For working with large data sets Go, MongoDB, and mgo are a powerful combination.

Authcookie is a web library for generating and verifying user authentication cookies. It allows web servers to hand out cryptographically secure tokens tied to a specific user that will expire after a specified time period. It has a simple API that makes it straightforward to add authentication to existing web applications. See the README file for details and example code.

Go-charset provides support for converting between Go's standard UTF-8 encoding and a variety of character sets. The go-charset package implements a translating io.Reader and io.Writer so you can wrap existing Readers and Writers (such as network connections or file descriptors), making it easy to communicate with systems that use other character encodings.

Go-socket.io is a Go implementation of Socket.IO, a client/server API that allows web servers to push messages to web browsers. Depending on the capabilities of the user's browser, Socket.IO uses the best transport for the connection, be it modern websockets, AJAX long polling, or some other mechanism. Go-socket.io bridges the gap between Go servers and rich JavaScript clients for a wide range of browsers. To get a feel for go-socket.io see the chat server example.

It's worth mentioning that these packages are goinstallable. With an up-to-date Go installation you can install them all with a single command:

goinstall launchpad.net/mgo \
    github.com/dchest/authcookie \
    go-charset.googlecode.com/hg/charset \
    github.com/madari/go-socket.io

Once goinstalled, the packages can be imported using those same paths:

import (
    "launchpad.net/mgo"
    "github.com/dchest/authcookie"
    "go-charset.googlecode.com/hg/charset"
    "github.com/madari/go-socket.io"
)

Also, as they are now a part of the local Go system, we can inspect their documentation with godoc:

godoc launchpad.net/mgo Database # see docs for Database type

Of course, this is just the tip of the iceberg; there are more great Go libraries listed on the package dashboard and many more to come.

Andrew Gerrand 编写

相关文章