Work with paths in Go - without tiresome error handling
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Initial commit Dec 9, 2015
.travis.yml Support go1.6 and go1.7 Aug 28, 2016
LICENSE rename section "Enhanced Usage" Dec 31, 2015
assert.go Initial commit Dec 9, 2015
doc.go doc_test.go Dec 14, 2015
doc_test.go doc_test.go Dec 14, 2015
gopath.go add GoPath.PrependErr(string) func Aug 28, 2016
predicate.go Initial commit Dec 9, 2015
terminal.go Added Components() as terminal func Dec 28, 2015
terminal_test.go Added Components() as terminal func Dec 28, 2015
transform.go fix tests; add ToSlash() func Aug 28, 2016
transform_test.go fix tests; add ToSlash() func Aug 28, 2016
utils_test.go fix tests; add ToSlash() func Aug 28, 2016


A Go library for nicer ways to cope with errors while working on the filesystem.

Do the work first -- check for errors later.

This library realizes the recommondations from the article Errors are values. For even more information, also have a look at Error handling and Go.

Build Status GoDoc


Instead of doing a lot of error handling between each step ...

var err error
var p = "/my/path/to/somewhere"

if p, err = filepath.Abs(p); err != nil {
  // handle error
if p, err = filepath.EvalSymlinks(p); err != nil {
  // handle error
if p, err = filepath.Rel(p, "/other/path"); err != nil {
  // handle error

// go on

...GoPath allows you to write the operations first, and do the error checking later.

var p = gopath.FromPath("/my/path/to/somewhere").Abs().EvalSymlinks().Rel(otherPath)

if p.HasErr() {
  // handle error

// go on

The idea: the GoPath object encapsulates a path and an error object. Now, as soon as an error occured, each operation turns to a no-op. That's why it suffices to check for errors in the end.

So, just import gopath and go ahead:

import ""

We don't pull thirdparty libs into your project.


GoPath has an extension mechanism that allows you to chain function calls with your own code. The first step is to define your own transformation function:

func normalizePath(p gopath.GoPath) gopath.GoPath {
	return p.Abs().Clean()

Now, you are able to use GoPath's Do method:

var p = gopath.FromPath("/some/path").Do(normalizePath)


While this project embeds functions from the os, path and filepath packages, it is far from being complete. Pull requests are welcome!

Please note, that I split the functionality into several files. I hope you find their names to be quite self-explanatory. We have

  • gopath.go: typedef, constructor and elementary functions
  • err.go: functionality around error handling
  • transform.go: transformations from GoPath to GoPath objects
  • terminal.go: transformations from GoPath to other values
  • predicate.go: functions on GoPaths with boolean results
  • assert.go: assertions just return an error object if they fail

Related Work

The GitHub project go-on/queue also provides a way for elegant error handling in Go, but is not restricted to path operations. It rather allows you to execute any sequence of functions with separated error handling. However, that approach needs some amount of extra code and may reduce code readability, while this GoPath aims to support a readable notation of path operations.

License (MIT)

Licensed under the MIT License, see LICENSE file for more information.