path

Guided tour · I/O & Files · pkg.go.dev →

Slash-separated path manipulation. For URLs and io/fs paths. NOT for OS files — use path/filepath for that.

When to use path vs path/filepath

path always uses /. path/filepath uses the OS separator (\ on Windows). URL paths, io/fs paths, and embed.FS keys are always forward slashes — use path for those. Local disk paths — use filepath.

The usual helpers

Join, Dir, Base, Ext, Clean

path.Join("a", "b", "c")   // "a/b/c"
path.Dir("a/b/c.txt")      // "a/b"
path.Base("a/b/c.txt")     // "c.txt"
path.Ext("c.tar.gz")       // ".gz"
path.Clean("a//b/../c/")   // "a/c"

Match — glob, but / never matches *

ok, _ := path.Match("*.go", "main.go")       // true
ok, _ = path.Match("*.go", "cmd/main.go")     // false