pathKit

package
v2.8.107 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package pathKit

@Author Richelieu @Description 主要是对"path"、"path/filepath"的封装.

PS: filepath标准库的使用可以参考: https://www.cnblogs.com/jkko123/p/6923962.html

Index

Constants

This section is empty.

Variables

View Source
var (
	// SelfDir returns absolute directory path of current running process(binary).
	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
)
View Source
var GetOsTempDir func() string = os.TempDir

GetOsTempDir 获取系统的临时目录.

Deprecated: 不建议向系统临时目录中放东西(服务器不一定会给权限).

e.g. Windows: "C:\Users\Lenovo\AppData\Local\Temp" Mac: "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/"

View Source
var Walk func(root string, fn filepath.WalkFunc) error = filepath.Walk

Walk 遍历目录.

Deprecated: 使用 WalkDir.

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 完全跳过目录读取。如果目录读取失败,则该函数会再次为该目录调用一次以报告错误.
View Source
var WalkDir func(root string, fn fs.WalkDirFunc) error = filepath.WalkDir

Functions

func ChangeWorkingDir

func ChangeWorkingDir(dir string) error

ChangeWorkingDir 设置 当前工作目录的绝对路径

func CheckSkip

func CheckSkip(parent, path string) bool

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

func ExpandTilde(path string) (string, error)

ExpandTilde "~"(波浪号) => 当前用户主目录

e.g. ("") => "", nil ("~") => "/Users/richelieu", nil ("~/a/b/c") => "/Users/richelieu/a/b/c", nil

func FromSlash

func FromSlash(path string) string

FromSlash 将路径中的"/"替换为路径分隔符

func GetHomeDir

func GetHomeDir() string

GetHomeDir 获取当前用户的home dir(当前用户主目录)

func GetTempDir

func GetTempDir() (string, error)

GetTempDir 获取 本依赖 的专属临时目录.

PS: 不建议向系统临时目录中放东西(服务器不一定会给权限).

e.g. Mac
	() => "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/$$chimera", nil

func GetWorkingDir

func GetWorkingDir() string

GetWorkingDir 获取 当前工作目录的绝对路径

func Join

func Join(eles ...string) string

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 added in v2.1.7

func ReviseWorkingDirInTestMode(projectName string) (string, error)

ReviseWorkingDirInTestMode 适用于: 测试模式(_test.go)

@return 修改后的WorkingDir + error

func SetTempDir added in v2.2.39

func SetTempDir(path string) error

func Split

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

Split 分割路径中的目录与文件.

e.g. Mac ("/Users/richelieu/Downloads/") => "/Users/richelieu/Downloads/", "" ("/Users/richelieu/Downloads") => "/Users/richelieu/", "Downloads"

func SplitList

func SplitList(path string) []string

SplitList 使用"路径列表分隔符"将路径分开.

os.PathListSeparator(linux下默认为':',windows下为';')

e.g. ("d:/a/b/c.docx") => [d:/a/b/c.docx] ("C:/windows;C:/windows/system") => [C:/windows C:/windows/system]

func ToSlash

func ToSlash(path string) string

ToSlash 将路径分隔符使用"/"替换

Types

This section is empty.

Jump to

Keyboard shortcuts

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