Go Reference ยท Errors, I/O and Files
Go path/filepath package
Reference for Go path/filepath: Join, Base, Dir, Ext, Clean, Abs, Rel, Split, Match, Glob and EvalSymlinks, with OS-specific separators explained.
The path/filepath package manipulates file names the way the current operating system writes them: / separators on Linux and macOS, \ and drive letters on Windows. It works purely on strings (except Abs, Glob, EvalSymlinks and WalkDir, which touch the file system), so it never checks that a path exists.
Use filepath for paths on disk. Use the sibling package path for slash-separated paths that are not file names, such as URL paths and the names inside io/fs file systems or embed.
p := filepath.Join("data", "reports", "2026", "q1.csv") // data/reports/2026/q1.csv (data\reports\2026\q1.csv on Windows)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Join(elem ...string) string func Split(path string) (dir, file string) func Base(path string) string func Dir(path string) string func Ext(path string) string func Clean(path string) string func Abs(path string) (string, error) func Rel(basepath, targpath string) (string, error) func IsAbs(path string) bool func IsLocal(path string) bool func Match(pattern, name string) (matched bool, err error) func Glob(pattern string) (matches []string, err error) func EvalSymlinks(path string) (string, error) func WalkDir(root string, fn fs.WalkDirFunc) error func Localize(path string) (string, error) func VolumeName(path string) string func SplitList(path string) []string func ToSlash(path string) string func FromSlash(path string) string const Separator = os.PathSeparator
| Function | Behavior |
|---|---|
Join |
Joins with the OS separator, skips empty elements and cleans the result. |
Base |
Last element; "" gives ".", trailing separators are removed. |
Dir |
Everything but the last element, cleaned; no directory gives ".". |
Ext |
The suffix starting at the last dot in the final element (including the dot), or "". |
Clean |
Removes ., resolves .. lexically, collapses repeated separators. |
Rel |
Relative path from basepath to targpath; error if impossible (different volumes or an unresolvable ..). |
Match |
Shell-style patterns: * (no separators), ?, [a-z], [^x], and a backslash to escape (not on Windows). Returns ErrBadPattern for malformed patterns. |
Glob |
Files matching the pattern; ignores I/O errors, returns ErrBadPattern only for bad syntax. There is no **. |
IsLocal |
Reports whether a path stays inside the current directory (Go 1.20); useful for validating untrusted names. |
Usage notes
- Never build paths with
+orfmt.Sprintf("%s/%s"); useJoin, which handles separators and... Cleanis lexical.a/../bbecomesbeven ifais a symbolic link elsewhere. UseEvalSymlinksto resolve real links.Extreturns only the last extension:archive.tar.gzgives.gz.- Path traversal: joining an untrusted name such as
../../etc/passwdonto a base directory escapes it. Validate withfilepath.IsLocal, or useos.Root(Go 1.24) so the OS enforces the boundary. Walkis older thanWalkDirand slower, because it callsLstaton every file. PreferWalkDir.GlobandMatchuse the separator of the OS, so on Windows the backslash cannot be used as an escape character.- Slash-separated paths:
filepath.ToSlashandFromSlashconvert between forms, for example when reading names fromembedorfs.FS. - Symlink-aware size or type checks need
os.Lstat;filepathitself does not look at the file system for most functions.