pathKit

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 9 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

This section is empty.

Functions

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 Clean

func Clean(path string) string

Clean 返回等价的最短路径(清理路径)

PS: 可用于去掉路径中的 "./" 、 "../" ...

e.g. ("./1.txt") => "1.txt" ("/root/.././c") => "/c"

func EvalSymlinks(path string) (string, error)

EvalSymlinks 返回链接文件的实际路径

@param path e.g."1.lnk"

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 GetAbsolutePath

func GetAbsolutePath(path string) (string, error)

GetAbsolutePath 获取的绝对路径(传参path相对于当前路径(os.Getwd())).

func GetHomeDir

func GetHomeDir() string

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

func GetOutputPath

func GetOutputPath(timeStr string) (string, error)

GetOutputPath 获取输出目录的路径

func GetParentDir

func GetParentDir(nameOrPath string) string

GetParentDir 返回文件(或目录)路径的父路径.

PS: 类似于Java中的 getParentFile().

@param nameOrPath 文件名或文件路径

e.g. ("") => "." (".") => "." ("yozo.eio") => "." ("/") => "/"

e.g.1 Mac ("./a/b/c") => "a/b" ("C:/a/b/c") => "C:/a/b"

func GetProjectDir

func GetProjectDir() string

func GetRelativePath

func GetRelativePath(basePath, targetPath string) (string, error)

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

func GetTempDir

func GetTempDir() string

GetTempDir 获取系统的临时目录.

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

func GetUniqueTempDir added in v1.3.11

func GetUniqueTempDir() (string, error)

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

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

func GetUserHomePath

func GetUserHomePath() string

func Glob

func Glob(pattern string) (matches []string, err error)

Glob (正则相关)返回所有匹配的文件.

e.g. ("d:/test/*.txt") => ([d:\test\a.txt d:\test\b.txt], <nil>)

func IsAbs

func IsAbs(path string) bool

IsAbs 判断路径是不是绝对路径

PS: 传参path 对应的文件(或目录)可以不存在.

e.g. ("./a/b/c") => false ("C:/a/b/c") => true ("/root") => true ("root") => false

func IsAbsolutePath

func IsAbsolutePath(path string) bool

IsAbsolutePath 是否绝对路径?

e.g. ("") => false ("./a/b/c") => false ("/a/b/c") => true

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 Match

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

Match (正则相关)匹配文件名,完全匹配则返回true

e.g. ("*", "a") => (true, <nil>) ("*", "C:/a/b/c") => (true, <nil>) ("\\b", "b") => (false, <nil>)

func SetProjectDir

func SetProjectDir(dir 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 将路径分隔符使用"/"替换

func VolumeName

func VolumeName(path string) string

VolumeName 返回分区名

PS: 非Windows平台,将返回"".

e.g. Windows平台 ("C:/a/b/c") => "C:"

Types

This section is empty.

Jump to

Keyboard shortcuts

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