Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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. Joining Paths:

    • filepath.Join("dir1", "dir2", "filename") joins path elements to form a path.

  2. Handling Extra Separators:

    • filepath.Join("dir1//", "filename") demonstrates joining with extra separators.

    • filepath.Join("dir1/../dir1", "filename") resolves ".." in the path.

  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. 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. Extracting Extension:

    • filepath.Ext(filename) extracts the extension from a filename.

  6. Trimming Suffix:

    • strings.TrimSuffix(filename, ext) removes the extension from a filename.

  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.

PreviousLine FiltersNextDirectories

Last updated 1 year ago

Was this helpful?