02 Jan 2010

When Go launched a couple months back, it came with a pretty good
collection of packages . I’ve been playing
with the language more or less since it launched, and in that time I’ve
written code that’s covered quite a few of these packages. While there
are a bunch of weird ones that I’ve never used, there are several that
I seem to use repeatedly. So in no particular
order, these are the five most useful go packages I’ve found.

bytes.Buffer is probably the most useful type for working with
data. It’s a dynamic-size byte buffer that lets you read and write
strings or bytes. At any time you can get the underlying data in either string or byte array format. It also implements several recurring Go interfaces
(io.Reader, io.Writer) which makes it usable as buffers in many other
packages. I’ve found it particularly useful in the following situations:

Reflect is a powerful package that lets you do runtime type inspection. Although Go is a statically typed language, many functions can work with generic data through the use of the generic type : interface{}. This is especially useful in packages that do serialization or data storage.

For instance, we can write a simple method that prints some details about the argument:

If you need to make a system call, you’re going to need to import this
package. Anything that involves calling the underlying system – like like forking processes, executing commands,
reading/writing files, opening directories, or grabbing environment variables – go through the os package.