Documentation ¶
Overview ¶
Package path provides methods for working with paths (fully compatible with base path package)
Index ¶
- Variables
- func Base(path string) string
- func Clean(path string) string
- func Dir(path string) string
- func DirN(path string, n int) string
- func Ext(path string) string
- func IsAbs(path string) bool
- func IsDotfile(path string) bool
- func IsSafe(path string) bool
- func Join(elem ...string) string
- func Match(pattern, name string) (matched bool, err error)
- func Split(path string) (dir, file string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadPattern = errors.New("Syntax error in pattern")
ErrBadPattern indicates a globbing pattern was malformed
Functions ¶
func Clean ¶
Clean returns the shortest path name equivalent to path by purely lexical processing
Example ¶
fmt.Println(Clean("/project//abc"))
Output: /project/abc
func Dir ¶
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 ¶
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("Bin dir: %s\n", DirN(path2, 2))
Output: Config dir: /home/user/project/config Bin dir: /usr/sbin
func Ext ¶
Ext returns the file name extension used by path
Example ¶
fmt.Println(Ext("/home/user/file.zip"))
Output: .zip
func IsAbs ¶
IsAbs reports whether the path is absolute
Example ¶
fmt.Println(IsAbs("/dev/null"))
Output: true
func IsDotfile ¶
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 IsSafe ¶
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 ¶
Join joins any number of path elements into a single path, adding a separating slash if necessary
Example ¶
fmt.Println(Join("home", "user", "project"))
Output: home/user/project
Types ¶
This section is empty.