go语言中处理路径的标准库--filepath

ToSlash

函数: func ToSlash(path string) string

释义: 将 path 中平台相关的路径分隔符转换为 /

例子:

s := "F:\\golang_workspace\\leetcode\\aa.js"
fmt.Println("path: ", s)

// ToSlash 将 path 中平台相关的路径分隔符转换为 '/'
s = filepath.ToSlash(s)
fmt.Println("ToSlash: ", s)
// ToSlash:  F:/golang_workspace/leetcode

FromSlash

函数: func FromSlash(path string) string

释义: 将 path 中的 / 转换为系统相关的路径分隔符

例子:

// FromSlash 将 path 中的 '/' 转换为系统相关的路径分隔符
s = filepath.FromSlash(s)
fmt.Println("FromSlash: ", s)
// FromSlash:  F:\golang_workspace\leetcode

Dir

函数: func Dir(path string) string

释义: 获取path中最后一个分隔符之前的部分(不包含分隔符)

例子:

// Dir 获取 path 中最后一个分隔符之前的部分(不包含分隔符)
s = "/golang_workspace/leetcode/aa.js"
s = filepath.Dir(s)
fmt.Println("Dir: ", s)
// Dir:  \golang_workspace\leetcode

Base

函数: func Base(path string) string

释义: 获取path中最后一个分隔符之后的部分(不包含分隔符)

例子:

// Base 获取path中最后一个分隔符之后的部分(不包含分隔符)
s = "/golang_workspace/leetcode/aa.js"
s = filepath.Base(s)
fmt.Println("Base: ", s)
// Base:  aa.js

Split

函数:func Split(path string) (dir, file string)

释义: 获取path中最后一个分隔符前后的两部分

例子:

// Split 获取 path 中最后一个分隔符前后的两部分
// 之前包含分隔符,之后不包含分隔符
s = "/golang_workspace/leetcode/aa.js"
d, s := filepath.Split(s)
fmt.Println("Split: ", d, s)
// Split:  /golang_workspace/leetcode/ aa.js

Ext

函数:func Ext(path string) string

释义: 获取路径字符串中的文件扩展名

例子:

// EXT 获取路径字符串中的文件扩展名
s = "/golang_workspace/leetcode/aa.js"
s = filepath.Ext(s)
fmt.Println("Ext: ", s)
// Ext:  .js

Rel

函数:func Rel(basepath, targpath string) (string, error)

释义: 获取targpath相对于basepath的路径

要求targpaht和basepath必须 都是相对路径都是绝对路径

例子:

// Rel 获取 targpath 相对于 basepath 的路径。
s = "/golang_workspace/leetcode/aa.js"
s2 := "/golang_workspace/"
s, _ = filepath.Rel(s2,s)
fmt.Println("Rel: ", s)
// Rel:  leetcode\aa.js

Join

函数:func Join(elem ...string) string

释义: 将elem中的多个元素合并成一个路径,忽略空元素,清理多余字符

例子:

// Join 将 elem 中的多个元素合并为一个路径,忽略空元素,清理多余字符。
s = "golang_workspace"
s2 = "leetcode/aa.js"
s = filepath.Join(s,s2)
fmt.Println("Join: ", s)
// Join:  golang_workspace\leetcode\aa.js

Clean

函数: func Clean(path string) string

释义: 清除path中多余的字符

例子:

// Clean 清理路径中的多余字符,比如 /// 或 ../ 或 ./
//返回等价的最短路径
//1.用一个斜线替换多个斜线
s = filepath.Clean("/...//....//abc//abc")
// \...\....\abc\abc
//2.清除当前路径.
s = filepath.Clean("./1.txt")
// 1.txt
s = filepath.Clean("/../../abc/abc")
//\abc\abc
//3.清除内部的..和他前面的元素
s = filepath.Clean("C:/a/b/../c")
// C:\a\c
//4.以/..开头的,变成/
s = filepath.Clean("/../1.txt")
fmt.Println("Clean: ", s)
// Clean:  \1.txt

IsAbs

函数:func IsAbs(path string) (b bool)

释义: 判断该路径是否是绝对路径

例子:

// 判断路径是否为绝对路径
s = "/home/gopher"
s2 = ".bashrc"
f := filepath.IsAbs(s)
f = filepath.IsAbs(s2)
fmt.Println("IsAbs: ", f)
// IsAbs:  true  IsAbs:  false

Abs

函数:func Abs(path string) (string, error)

释义: 获取path的绝对路径

例子:

// 返回所给目录的绝对路径
s = ".bashrc"
s,_ = filepath.Abs(s)
fmt.Println("Abs: ", s)
// Abs:  F:\golang_workspace\GoLearn\.bashrc

SplitList

函数:func SplitList(path string) []string

释义: 按os.PathListSeparator即(;)将路径进行分割

例子:

// 将路径序列 操作系统特别的连接符组成的path
s = "/a/b/c:/usr/bin"
sList := filepath.SplitList(s)
fmt.Println("SplitList: ", sList)
// SplitList:  [/a/b/c:/usr/bin]

VolumeName

函数:func VolumeName(path string) string

释义: 返回路径字符串中的卷名

例子:

// 返回路径字符串中的卷名
s = "F:\\golang_workspace\\leetcode\\aa.js"
s = filepath.VolumeName(s)
fmt.Println("VolumeName: ", s)
// VolumeName:  F:

Match

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

释义: 根据pattern来判断name是否匹配,如果匹配则返回true

例子:

// 根据pattern来判断name是否匹配,如果匹配则返回true
r := "/home/catch/*"
s = "/home/catch/foo"
f, _ = filepath.Match(r, s)
fmt.Println("Match: ", f)
// Match:  true

Glob

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

释义: 列出与指定的模式 pattern 完全匹配的文件或目录(匹配原则同上)

例子:

// 列出与指定的模式 pattern 完全匹配的文件或目录(匹配原则同match)
r = "F:\\golang_workspace\\[s]*"
sList,_ = filepath.Glob(r)
fmt.Println("Glob: ", sList)
// Glob:  [F:\golang_workspace\shenqi_server F:\golang_workspace\src]

Walk

函数:func Walk(root string, walkFn WalkFunc) error

释义: 遍历指定目录(包括子目录),对遍历的项目用walkFn函数进行处理

例子:

// 遍历指定目录(包括子目录),对遍历的项目用walkFn函数进行处理
pwd,_ := os.Getwd()
filepath.Walk(pwd,func(fpath string, info os.FileInfo, err error) error {
	if match,err := filepath.Match("???",filepath.Base(fpath)); match {
		fmt.Println("Walk path:",fpath)
		fmt.Println("Walk info:",info)
		return err
	}
	return nil
})
// Walk path:  F:\golang_workspace\GoLearn\src
posted @   厚礼蝎  阅读(522)  评论(0编辑  收藏  举报
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示