Skip to content

Learning Go – Miniblog #4 – Errors

January 10, 2013

This carries on from here.

Flushed with the success of my last Go coding attempt, I decided to write something to dump text files with line numbers. I’m definitely going to be able to read files  (and directories, I’ll get to that later). The obvious place to start looking for how to do this is in the documentation for the Go os package, which provides the interface to the operating system. This looks to be a rather thin wrapper to the C Standard Library, which kind of surprised me, as I’ve never felt the CSL was a thing  of great design. My heart sank a bit too when it seemed there was no simple readline function that would handle the allocation of memory for the line -surely they didn’t expect me to write this incredibly commonly used bit of functionality from scratch?

As it turns out, no they didn’t. A bit of poking about revealed the bufio package which handles buffering while reading from things like files. The four important functions I was going to have to use were the os.Open() function, which opens a file for reading, and gives you a file pointer, the bufio.NewReader() function that creates a buffered reader, the ReadString() method of the reader, which reads a line of text, and the Close() method of the file, which closes the file.

My first attempt to open a file looked something like this:

file := os.Open( "lines.txt" )

but this won’t work. The Open() function, like many Go functions, does not return a single value, it returns two values – the opened file pointer, and an error indicator. Go doesn’t support exceptions, so everything seems to be done through these indicators. So the open code needs to look something like this:

file,err := os.Open( fname )
if err != nil {
    // handle error somehow
// do something with file

The ReadString() function will also return an error value if it cannot read a line (i.e. we are probably at the end of file). Bearing this in mind, I came up with the following function which opens a named file and lists its contents with line numbers:

func listfile( fname string ) error {
    file,err := os.Open( fname )
    if err != nil {
        return err
    reader := bufio.NewReader( file )
    lineno := 1
    for  {
        line,err := reader.ReadString( '\n' )
        if err == nil {
            fmt.Printf( "%d: %s", lineno, line )
        } else {
    return nil


This is a bit longer than I would have liked, and I’m not sure I’ve dealt with all the possible errors in the right way, but it does at least "work". Next task, produce a directory listing…


From → golang

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: