This article introduces Go standard library: Path, Filepath commonly used export functions, structures and methods

1. The path

import "path"
Copy the code

Path implements functions that operate on paths separated by slashes.

  • func IsAbs(path string) bool// Check if it is an absolute path
  • func Split(path string) (dir, file string)// Split the path into a path and a file name
  • func Join(elem ... string) string// Merge multiple strings into a single path
  • func Ext(path string) string// Return to the extension in the path
  • func Base(path string) string// Returns the last element of the path
  • func Dir(path string) string// Return to the directory part of the path
  • func Clean(path string) string// Returns the shortest path to the same directory
  • func Match(pattern, name string) (matched bool, err error)// Whether the re matches the path (shell file name matches)

$GOPATH/src/github.com/ironxu/go_note/library/path/path.go code is as follows:

Path package main import (" FMT ""path") func main() {pt := "~/gocode/ SRC /go_note/package/path" // Is_abs := path.isabs (pt) fmt.Println(is_abs) // false // Splits the path into a path and a filename pf := "~gocode/src/go_note/package/path/path.go" dir, file := path.Split(pf) fmt.Println(dir, // ~gocode/ SRC /go_note/package/path/ path.go // Merge multiple strings into one path dir_join := path.Join("usr", "local", "Bin ") fmt.println (dir_join) /usr/local/bin// Returns the extension file_ext in the path := path.ext (pf) fmt.println (file_ext) //.go // Dir_base := path.base (pf) fmt.println (dir_base) // path.go // Return the directory part of the path dir = path.dir (pf) fmt.println (dir) // ~gocode/ SRC /go_note/package/path // Returns the shortest path to the same directory dir_a := "/usr/.. /etc/.. /etc/ssh" fmt.Println(path.clean (dir_A)) // etc/ ssh/regex whether to Match path is_match, err := path.match ("*.xml", "a.xml") fmt.Println(is_match, err) // true < nil> }Copy the code

2. The path/filepath package

import "path/filepath"
Copy the code

The filepath package implements filepath manipulation functions compatible with various operating systems.

  • filepath.Separator// A predefined variable that represents the path separator/
  • filepath.ListSeparator// Predefined variables, representing environment variable separators:
  • func Abs(path string) (string, error)// Returns the absolute path relative to the current path
  • func Clean(path string) string// Return the shortest path of path
  • func Rel(basepath, targpath string) (string, error)// Return targpath relative to Basepath
  • func EvalSymlinks(path string) (string, error)// Return the path to which the soft chain points
  • func VolumeName(path string) string// Returns the volume name at the beginning of the path
  • func ToSlash(path string) string// Replace the path separator with/
  • func FromSlash(path string) string // /Replace it with a path separator
  • func SplitList(path string) []string// Separate the paths inside the environment variables
  • func Walk(root string, walkFn WalkFunc) error// Traverse the file tree in the root directory and call walkFn

$GOPATH/src/github.com/ironxu/go_note/library/path/filepath.go code is as follows:

// Go standard library path/filepath package main import (" FMT "" OS" "path/filepath") info os.FileInfo, err error) error { if err ! = nil {return err} else {fmt.println (path)} return nil} func main() {// Predefined variable fmt.Println(string(filepath.Separator), String (filepath.ListSeparator)) // Returns the absolute path relative to the current path dir := "~/gocode/ SRC /go_note/package/filepath" real_dir, Err := filepath.Abs(dir) fmt.Println(real_dir, err) // Returns the shortest path dir = "/usr/.. /etc/.. / TMP "clear_dir := filepath.Clean(dir) fmt.Println(clear_dir) // / TMP // Return targpath to basepath. targpath := "/usr/local", "/usr/local/go/bin" rel_dir, err := filepath.Rel(basepath, targpath) fmt.Println(rel_dir, err) // go/bin < nil> Symlink := "/usr/local" real_dir, err = filepath.EvalSymlinks(symlink) fmt.println (real_dir, err) // /usr/local < nil> // return the VolumeName at the beginning of the path root := "/usr/local/go" vol := filepath.VolumeName(root) ftt.println (vol) // "/ / replace the path separator with '/' slash_dir ToSlash(root) FMT.Println(slash_dir) // /usr/local/go // '/' replace from_slash := Filepath.FromSlash(slash_dir) fmt.Println(from_slash) // /usr/local/go // Delimit the path in the environment variable env_path := "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/go/bin" env_slice := filepath.SplitList(env_path) for k, v := range env_slice { fmt.Println(k, v) } // 0 /usr/local/bin // 1 /usr/bin // 2 /bin // 3 /usr/sbin // 4 /sbin // 5 /opt/X11/bin // 6 /usr/local/go/bin // WalkFn root_dir, err := os.getwd () err = filepath.Walk(root_dir, pathName) fmt.println (err)}Copy the code

reference

  • PKG/path in Chinese
  • PKG/path_filepath Chinese
  • pkg/path
  • pkg/filepath