Skip to content

path Module

The path module provides functions for working with file paths.

Functions

Join(parts...)

Joins any number of path elements into a single path.

Parameters: - parts...: The path elements to join.

Returns: - (string, error): The joined path.

Example:

Join Example
1
2
3
4
5
import fmt
import path

var p, _ = path.Join("/home", "user", "file.txt")
fmt.Println(p) // Output: /home/user/file.txt


Dir(path)

Returns the directory portion of a path.

Parameters: - path: The file path.

Returns: - (string, error): The directory portion.

Example:

Dir Example
1
2
3
4
5
import fmt
import path

var dir, _ = path.Dir("/home/user/file.txt")
fmt.Println(dir) // Output: /home/user


Base(path)

Returns the last element of a path.

Parameters: - path: The file path.

Returns: - (string, error): The last element.

Example:

Base Example
1
2
3
4
5
import fmt
import path

var base, _ = path.Base("/home/user/file.txt")
fmt.Println(base) // Output: file.txt


Ext(path)

Returns the file extension of a path.

Parameters: - path: The file path.

Returns: - (string, error): The file extension.

Example:

Ext Example
1
2
3
4
5
import fmt
import path

var ext, _ = path.Ext("/home/user/file.txt")
fmt.Println(ext) // Output: .txt


Abs(path)

Returns an absolute representation of a path.

Parameters: - path: The file path.

Returns: - (string, error): The absolute path.

Example:

Abs Example
1
2
3
4
5
import fmt
import path

var abs, _ = path.Abs("file.txt")
fmt.Println(abs)


Clean(path)

Returns the shortest path name equivalent to path by purely lexical processing.

Parameters: - path: The file path.

Returns: - (string, error): The cleaned path.

Example:

Clean Example
1
2
3
4
5
import fmt
import path

var cleaned, _ = path.Clean("/foo//bar/../baz/./")
fmt.Println(cleaned) // Output: /foo/baz


IsAbs(path)

Reports whether a path is absolute.

Parameters: - path: The file path.

Returns: - (boolean, error): true if absolute, else false.

Example:

IsAbs Example
1
2
3
4
5
import fmt
import path

var abs, _ = path.IsAbs("/usr/local")
fmt.Println(abs) // Output: 1 (true)


Split(path)

Splits a path immediately following the final separator, returning directory and file name.

Parameters: - path: The file path.

Returns: - (string, string, error): (dir, file).

Example:

Split Example
1
2
3
4
5
6
import fmt
import path

var dir, file, _ = path.Split("/home/user/file.txt")
fmt.Println(dir)  // Output: /home/user/
fmt.Println(file) // Output: file.txt


Rel(base, target)

Returns a relative path that is lexically equivalent to target when joined to base.

Parameters: - base: Base path. - target: Target path.

Returns: - (string, error): The relative path.

Example:

Rel Example
1
2
3
4
5
import fmt
import path

var rel, _ = path.Rel("/home/user", "/home/user/docs/file.txt")
fmt.Println(rel) // Output: docs/file.txt


ToSlash(path)

Replaces OS-specific separators with /.

Parameters: - path: Input path.

Returns: - (string, error)

Example:

ToSlash Example
1
2
3
4
5
import fmt
import path

var s, _ = path.ToSlash("C:\\Windows\\System32")
fmt.Println(s) // Output on Windows: C:/Windows/System32


FromSlash(path)

Replaces / with OS-specific separators.

Parameters: - path: Input path.

Returns: - (string, error)

Example:

FromSlash Example
1
2
3
4
5
import fmt
import path

var s, _ = path.FromSlash("/usr/local/bin")
fmt.Println(s)


Match(pattern, name)

Reports whether name matches the shell file name pattern.

Parameters: - pattern: Glob-style pattern (e.g. *.txt). - name: Name to match.

Returns: - (boolean, error)

Example:

Match Example
1
2
3
4
5
import fmt
import path

var ok, _ = path.Match("*.txt", "file.txt")
fmt.Println(ok) // Output: 1 (true)


Glob(pattern)

Returns the names of all files matching pattern, or an empty array if none.

Parameters: - pattern: Glob pattern.

Returns: - (array, error): Array of matching paths.

Example:

Glob Example
1
2
3
4
5
import fmt
import path

var matches, _ = path.Glob("*.txt")
fmt.Println(matches)


SplitList(pathList)

Splits a list of paths joined by the OS-specific ListSeparator (: on Unix, ; on Windows).

Parameters: - pathList (string): Path list string (e.g., from PATH environment variable)

Returns: - (array, error): Array of individual path strings.

Example:

SplitList Example
import fmt
import path
import os

// Process PATH environment variable
var pathEnv, _ = os.Getenv("PATH")
var paths, _ = path.SplitList(pathEnv)

fmt.Printf("Found %d paths in PATH:\n", len(paths))
for p in paths {
    fmt.Printf("  - %s\n", p)
}

// Manual path list
var manualList = "/usr/bin:/usr/local/bin:/bin"
var manualPaths, _ = path.SplitList(manualList)
fmt.Println(manualPaths)  // [/usr/bin, /usr/local/bin, /bin]


VolumeName(path)

Returns the leading volume name. On Windows, returns the drive letter (e.g., "C:"). On Unix systems, returns an empty string.

Parameters: - path (string): File path

Returns: - (string, error): Volume name or empty string.

Example:

Volume Name
import fmt
import path

// Windows paths
var vol1, _ = path.VolumeName("C:\\Windows\\System32")
fmt.Println(vol1)  // Windows: "C:", Unix: ""

var vol2, _ = path.VolumeName("D:\\Data\\files")
fmt.Println(vol2)  // Windows: "D:", Unix: ""

// Unix paths (no volume)
var vol3, _ = path.VolumeName("/usr/local/bin")
fmt.Println(vol3)  // Always: ""

// Use for cross-platform path handling
var fullPath = "C:\\projects\\myapp\\file.txt"
var volume, _ = path.VolumeName(fullPath)
if volume != "" {
    fmt.Printf("Windows path with volume: %s\n", volume)
} else {
    fmt.Println("Unix-style path (no volume)")
}

See Also