path

package
v12.115.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 7 Imported by: 10

Documentation

Overview

Package path provides methods for working with paths (fully compatible with base path package)

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = errors.New("Syntax error in pattern")

ErrBadPattern indicates a globbing pattern was malformed

Functions

func Base

func Base(path string) string

Base returns the last element of path

Example
fmt.Println(Base("/home/user/project"))
Output:

project

func Clean

func Clean(path string) string

Clean returns the shortest path name equivalent to path by purely lexical processing

Example
fmt.Println(Clean("/project//abc"))
Output:

/project/abc

func Compact added in v12.44.0

func Compact(path string) string

Compact converts path to compact representation (e.g /some/random/directory/file.txt → /s/r/d/file.txt)

Example
fmt.Println(Compact("/home/user/john/file.zip"))
Output:

/h/u/j/file.zip

func Dir

func Dir(path string) string

Dir returns all but the last element of path, typically the path's directory

Example
fmt.Println(Dir("/home/user/project"))
Output:

/home/user

func DirN

func DirN(path string, n int) string

DirN returns first N elements of path

Example
path1 := "/home/user/project/config/file.cfg"
path2 := "/usr/sbin/myapp"

fmt.Printf("Config dir (→): %s\n", DirN(path1, 4))
fmt.Printf("Config dir (←): %s\n", DirN(path1, -1))
fmt.Printf("Bin dir (→): %s\n", DirN(path2, 2))
fmt.Printf("Bin dir (←): %s\n", DirN(path2, -1))
Output:

Config dir (→): /home/user/project/config
Config dir (←): /home/user/project/config
Bin dir (→): /usr/sbin
Bin dir (←): /usr/sbin

func Ext

func Ext(path string) string

Ext returns the file name extension used by path

Example
fmt.Println(Ext("/home/user/file.zip"))
Output:

.zip

func IsAbs

func IsAbs(path string) bool

IsAbs reports whether the path is absolute

Example
fmt.Println(IsAbs("/dev/null"))
Output:

true

func IsDotfile

func IsDotfile(path string) bool

IsDotfile returns true if file name begins with a full stop

Example
file1 := "/home/user/project/file"
file2 := "/home/user/project/.file"

fmt.Printf("%s is dotfile → %t\n", file1, IsDotfile(file1))
fmt.Printf("%s is dotfile → %t\n", file2, IsDotfile(file2))
Output:

/home/user/project/file is dotfile → false
/home/user/project/.file is dotfile → true

func IsGlob

func IsGlob(pattern string) bool

IsGlob returns true if given pattern is Unix-like glob

Example
file1 := "file"
file2 := "*.file"

fmt.Printf("%s is glob → %t\n", file1, IsGlob(file1))
fmt.Printf("%s is glob → %t\n", file2, IsGlob(file2))
Output:

file is glob → false
*.file is glob → true

func IsSafe

func IsSafe(path string) bool

IsSafe returns true is given path is safe to use (not points to system dirs)

Example
path1 := "/home/user/project"
path2 := "/usr/sbin/myapp"

fmt.Printf("%s is safe → %t\n", path1, IsSafe(path1))
fmt.Printf("%s is safe → %t\n", path2, IsSafe(path2))
Output:

/home/user/project is safe → true
/usr/sbin/myapp is safe → false

func Join

func Join(elem ...string) string

Join joins any number of path elements into a single path, adding a separating slash if necessary

Example
fmt.Println(Join("/home/user", "project/app1", "main.go"))
Output:

/home/user/project/app1/main.go

func JoinSecure added in v12.67.0

func JoinSecure(root string, elem ...string) (string, error)

JoinSecure joins all elements of path, makes lexical processing, and evaluating all symlinks. Method returns error if final destination is not a child path of root.

Example
fmt.Println(Join("/home/user", "project/app1/../app2", "main.go"))
Output:

/home/user/project/app2/main.go

func Match

func Match(pattern, name string) (matched bool, err error)

Match reports whether name matches the shell file name pattern

Example
fmt.Println(Match("/home/*", "/home/user"))
Output:

true <nil>

func Split

func Split(path string) (dir, file string)

Split splits path immediately following the final slash, separating it into a directory and file name component

Example
fmt.Println(Split("/home/user/john/file.zip"))
Output:

/home/user/john/ file.zip

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL