Code with Go
Ask or search…
⌃K
Links

File Paths

This Go code demonstrates various functions from the filepath package for working with file paths. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
"fmt"
"path/filepath"
"strings"
)
​
// The main function, where the execution of the program begins.
func main() {
// Joining path elements to form a path.
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p)
​
// Joining path elements with extra separators.
fmt.Println(filepath.Join("dir1//", "filename"))
// Resolving ".." in the path.
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
​
// Extracting the directory and base name from a path.
fmt.Println("Dir(p):", filepath.Dir(p))
fmt.Println("Base(p):", filepath.Base(p))
​
// Checking if a path is absolute.
fmt.Println(filepath.IsAbs("dir/file"))
fmt.Println(filepath.IsAbs("/dir/file"))
​
// Extracting the extension from a filename.
filename := "config.json"
ext := filepath.Ext(filename)
fmt.Println(ext)
​
// Removing the extension from a filename.
fmt.Println(strings.TrimSuffix(filename, ext))
​
// Finding the relative path between two paths.
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
​
// Handling a case where the paths are not relative.
rel, err = filepath.Rel("a/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
}

Output

p: dir1\dir2\filename
dir1\filename
dir1\filename
Dir(p): dir1\dir2
Base(p): filename
false
false
.json
config
t\file
..\c\t\file
Explanation:
  1. 1.
    Joining Paths:
    • filepath.Join("dir1", "dir2", "filename") joins path elements to form a path.
  2. 2.
    Handling Extra Separators:
    • filepath.Join("dir1//", "filename") demonstrates joining with extra separators.
    • filepath.Join("dir1/../dir1", "filename") resolves ".." in the path.
  3. 3.
    Extracting Directory and Base Name:
    • filepath.Dir(p) extracts the directory from the path.
    • filepath.Base(p) extracts the base name (filename) from the path.
  4. 4.
    Checking if Path is Absolute:
    • filepath.IsAbs("dir/file") checks if the path is absolute (false).
    • filepath.IsAbs("/dir/file") checks if the path is absolute (true).
  5. 5.
    Extracting Extension:
    • filepath.Ext(filename) extracts the extension from a filename.
  6. 6.
    Trimming Suffix:
    • strings.TrimSuffix(filename, ext) removes the extension from a filename.
  7. 7.
    Relative Paths:
    • filepath.Rel("a/b", "a/b/t/file") finds the relative path between two paths.
    • filepath.Rel("a/b", "a/c/t/file") demonstrates handling a case where the paths are not relative.