18-06-2013, 01:02 PM
Go, The Standard Library
The Standard Library.pdf (Size: 166.7 KB / Downloads: 46)
Introduction
When I sit down to build a new piece of software in my favorite programming language of the week,
I open up my programmer’s toolbox. I can pull out a number of things, like my knowledge of the
language syntax and its quirks. It probably has some sort of library packaging system (rubygems¹
or python eggs²), and I have my list of libraries for doing certain jobs. The language also has a
standard library. All of these tools combine to help solve difficult programming
Right now, my programming language of choice is Go³ and it has a wonderful standard library. That
standard library is what this book is about.
I wanted to take an in depth look at something which normally doesn’t get a lot of press, and many
developers overlook. The standard library usually has a number of great solutions to problems that
you might be using some other dependency for, simply because you don’t about them. It makes no
sense for my application depend on an external library or program if the standard distribution of
the language has something built in.⁴
Learning the ins and outs of your favorite programming language’s standard library can help make
you a better programmer, and streamline your applications by removing dependencies. If this sounds
like something you’re interested in, keep reading.
Target Audience
This book is for people that know how to program Go already. It’s definitely not an intro. If you’re
completely new to Go, start with the documentation page⁵ and the reference page⁶. The language
specification is quite readable and if you’re already familiar with other programming languages you
can probably absorb the language from the spec.
If you know Go but want to step up your game and your usage of the standard library, this book is
for you.
Reading tar Files
Reading tar files is pretty straight forward. You use NewReader to get a handle to a Reader type.
Like NewWriter taking an io.Writer type, NewReader takes an io.Reader type, in order to plug into
other streams for reading tar files on the fly.
Once you have your Reader, you can iterate over the entries in the archive with the Next method.
It returns a Header and possibly and error. Remember to check the error since it’s used to signal the
end of the archive (with io.EOF) and other problems. Always check those errors!
You can read out an entry by calling Read on the reader you got back from NewReader, or pass it to a
utility function to read out the full contents of the entry. In the example, I use io.ReadFull to read
out the appropriate number of bytes into a slice, and can then print that to stdout.
builtin
Batteries Included
The builtin package isn’t a real package, it’s just here to document the builtin functions that come
with the language. Lower level than the standard library, these things are just…there. The builtins
let you do things with maps, slices, channels, and imaginary numbers, cause and deal with panics,
build objects, and get size information about certain things. Honestly, most of this can be learned
from the spec, but I’ve included it for completeness.
Building Objects
make
make is used to build the builtin types like slices, channels and maps. The first argument is the type,
and it can be one of those three types.
In the case of channels, there is an optional second integer parameter, the capacity. If it’s zero (or not
given), the channel is unbuffered. This means writes block until there is a reader ready to receive
the data, and reads block until there is a write ready to give data. If the parameter is greater than
zero, the channel is buffered with the capacity specified. On these channels, reads block only when
the channel is empty, and writes block only when the channel is full.
In the case of maps, the second parameter is also optional, but is rarely used. It controls the initial
allocation, so if you know exactly how big your map has to be, it can be helpful. cap (which we’ll see
later) doesn’t work on maps though, so you can’t really examine the effects of this second parameter
easily.