Documentation ¶
Overview ¶
Package pathKit
@Author Richelieu @Description 主要是对"path"、"path/filepath"的封装.
PS: filepath标准库的使用可以参考: https://www.cnblogs.com/jkko123/p/6923962.html
Index ¶
- Variables
- func ChangeWorkingDir(dir string) error
- func CheckSkip(parent, path string) bool
- func ExpandTilde(path string) (string, error)
- func FromSlash(path string) string
- func GetExclusiveTempDir() (string, error)
- func GetHomeDir() string
- func GetTempDir() string
- func GetWorkingDir() string
- func Join(eles ...string) string
- func ReviseWorkingDirInTestMode(projectName string) (string, error)
- func SetTempDir(dirPath string) error
- func Split(path string) (dir, file string)
- func SplitList(path string) []string
- func ToSlash(path string) string
Constants ¶
This section is empty.
Variables ¶
var ( // RealPath 获取给定路径的绝对路径地址. /* PS: 对应的文件或目录不存在的话,返回"". e.g. fmt.Println(pathKit.RealPath("")) // "/Users/richelieu/GolandProjects/chimera" fmt.Println(pathKit.RealPath(".")) // "/Users/richelieu/GolandProjects/chimera" fmt.Println(pathKit.RealPath("./")) // "/Users/richelieu/GolandProjects/chimera" */ RealPath func(path string) string = gfile.RealPath // SelfDir returns absolute directory path of current running process(binary). /* e.g. fmt.Println(pathKit.SelfDir()) // "/Users/richelieu/Library/Caches/JetBrains/GoLand2023.3/tmp/GoLand" */ SelfDir func() string = gfile.SelfDir // MainPkgPath returns absolute file path of package main, which contains the entrance function main. MainPkgPath func() string = gfile.MainPkgPath // ParentDir 返回文件(或目录)路径的父路径. /* PS: 类似于Java中的 getParentFile(). */ ParentDir func(path string) string = gfile.Dir // IsAbs 判断路径是不是绝对路径 /* PS: 传参path 对应的文件(或目录)可以不存在. e.g. ("./a/b/c") => false ("C:/a/b/c") => true ("/root") => true ("root") => false */ IsAbs func(path string) bool = filepath.IsAbs // Abs 绝对路径(传参path相对于当前路径(os.Getwd())). Abs func(path string) (string, error) = filepath.Abs // VolumeName 返回分区名 /* PS: 非Windows平台,将返回"". e.g. Windows平台 ("C:/a/b/c") => "C:" */ VolumeName func(path string) string = filepath.VolumeName // GetRelativePath 获取(targetPath 相对于 basePath 的)相对路径. /* e.g. Windows ("C:/a/b", "C:/a/b/c/d/../e") => "c\e", <nil> e.g.1 Mac ("/usr/local", "/usr/local/go/bin") => "go/bin", nil ("//usr////local", "/usr/local/go/bin") => "go/bin", nil ("//usr////local", "/usr/local/go/bin/../") => "go", nil */ GetRelativePath func(basepath, targpath string) (string, error) = filepath.Rel // Clean 返回等价的最短路径(清理路径) /* PS: 可用于去掉路径中的 "./" 、 "../" ... e.g. ("./1.txt") => "1.txt" ("/root/.././c") => "/c" */ Clean func(path string) string = filepath.Clean // Match (正则相关)匹配文件名,完全匹配则返回true /* e.g. ("*", "a") => (true, <nil>) ("*", "C:/a/b/c") => (true, <nil>) ("\\b", "b") => (false, <nil>) */ Match = filepath.Match // Glob (正则相关)返回所有匹配的文件. /* e.g. ("d:/test/*.txt") => ([d:\test\a.txt d:\test\b.txt], <nil>) */ Glob = filepath.Glob // EvalSymlinks 返回链接文件的实际路径. EvalSymlinks = filepath.EvalSymlinks )
var GetOsTempDir func() string = os.TempDir
GetOsTempDir 获取系统的临时目录.
PS: 不建议向系统临时目录中放东西(服务器不一定会给权限).
e.g.
Windows "C:\Users\Lenovo\AppData\Local\Temp" Mac "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/"
ReadDir 遍历目录.
PS: (1) 有子目录的话,"不会" 继续遍历; (2) "不会" 遍历到到传参name目录; (3) 例子可以参考 notes/Golang/Golang.wps.
Walk 遍历目录.
Deprecated: Use WalkDir instead.
PS: (1) 遍历包含 传参root; (2) filepath.Walk 和 filepath.WalkDir 的区别:
(a) filepath.WalkDir 是在 Go1.16 中引入的,比 filepath.Walk 更高效,filepath.WalkDir 避免了对每个访问的文件或目录调用 os.Lstat (b) 回调函数fn的传参不同,分别为 filepath.WalkFunc 和 fs.WalkDirFunc, 此外,WalkDirFunc 在读取目录之前调用,以允许 SkipDir 完全跳过目录读取。如果目录读取失败,则该函数会再次为该目录调用一次以报告错误.
var WalkDir func(root string, fn fs.WalkDirFunc) error = filepath.WalkDir
WalkDir 遍历目录.
PS: (1) 有子目录的话,"会" 继续遍历; (2) "会" 遍历到到传参root目录; (3) 例子可以参考 notes/Golang/Golang.wps.
Functions ¶
func CheckSkip ¶
CheckSkip 检查是否发生"路径穿越"(路径穿透)
@param path 父路径 @param path1 子路径 @return true: 发生"路径穿越"
e.g. ("/a//b/", "/a//b//../c.docx") => true ("/a//b/", "//a//b///c.docx") => false
func ExpandTilde ¶
ExpandTilde 拓展路径中的"~"("~"(波浪号) => 当前用户主目录).
e.g. ("") => "", nil ("~") => "/Users/richelieu", nil ("~/a/b/c") => "/Users/richelieu/a/b/c", nil
func GetExclusiveTempDir ¶
GetExclusiveTempDir 获取 本依赖 的专属临时目录.
PS: 不建议向系统临时目录中放东西(服务器不一定会给权限). e.g. macOS () => "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/$$chimera/cmnmk1pus0n0snnatg40 <nil>", nil
func Join ¶
Join 将多个字符串合并为一个路径(路径拼接).
PS: (1) 不建议用"path.Join",有问题,e.g. 传参:"d:\\nacos\\", "cyy" (2) 此方法也可用于优化路径(处理: 路径穿越、多个连续的路径分隔符...).
e.g. () => "" ("") => "" ("", "") => "" ("", "a") => "a"
(".", "yozo.eio") => "yozo.eio"
(" ") => " " (" ", "a") => " /a"
e.g.1 支持: 路径穿越(路径穿透) ("/a/b", "../c.docx") => "/a/c.docx"
func ReviseWorkingDirInTestMode ¶
ReviseWorkingDirInTestMode 适用于: 测试模式(_test.go)
@return 修改后的WorkingDir + error
func SetTempDir ¶
func Split ¶
Split 分割路径中的目录与文件.
e.g. Mac ("/Users/richelieu/Downloads/") => "/Users/richelieu/Downloads/", "" ("/Users/richelieu/Downloads") => "/Users/richelieu/", "Downloads"
Types ¶
This section is empty.