pathKit

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: Apache-2.0 Imports: 10 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())).

e.g. Windows ("./1.txt") => "E:\GolandProjects\go-scales\1.txt", nil

e.g.1 Mac ("a/b/c.xlsx") => "/Users/richelieu/GolandProjects/go-scales/a/b/c.xlsx"

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 GetTempDirOfGoScales

func GetTempDirOfGoScales() (string, error)

GetTempDirOfGoScales go-scales专属的临时目录

e.g. () => "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/$$go-scales", 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 ReviseProjectDirWhenTesting

func ReviseProjectDirWhenTesting() error

ReviseProjectDirWhenTesting 在测试(执行xxx_test.go文件)时,自动修改项目路径,将 projectDir 修改为 当前项目的根目录.

PS: (1) 执行xxx_test.go文件时,projectDir为该文件所在的目录,而非当前项目的根目录. (2) 想调用此方法的话,必须在程序启动时立即调用,以防来不及.

func SetProjectDir

func SetProjectDir(dir string) error

func Split

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

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

("d:/a/b/c.docx") => "d:/a/b/", "c.docx"

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