首页 > 其他分享 >go语言中处理路径的标准库--filepath

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

时间:2023-05-18 21:13:58浏览次数:33  
标签:string filepath -- fmt golang workspace go path

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

标签:string,filepath,--,fmt,golang,workspace,go,path
From: https://www.cnblogs.com/guangdelw/p/17413260.html

相关文章

  • systemd服务无法产生core文件
    解决思路:1、core的生成路径已被配置,确认非空/proc/sys/kernel/core_pattern2、core文件大小限制也去除了ulimit–c core文件的大小限制也设置成了unlimited3、/proc/sys/fs/suid_dumpable已设置为1,防止非本用户组的文件,无权限导致4、不使用systemctl启动进程,也能产生core......
  • DFS(深度优先搜索)
    代码:#include<bits/stdc++.h>usingnamespacestd;intm,n,p,q,minn=99999999;inta[100][100];//1为空,2为障碍intv[100][100];//0未访问,1已访问intdx[4]={0,1,0,-1};intdy[4]={1,0,-1,0};//设置遍历方向voiddfs(intx,inty,intstep){ if(x==p&&y==......
  • 【安全学习之路】Day3
    ......
  • 关于srand((unsigned)time(NULL))详解
    srand()函数用来设置算法的种子,time(NULL)返回当前的时间,先调用srand(time(NULL))是把当前的时间作为种子,是程序每次运行产生不同的随机数序列。涉及到三个概念:1、随机数:数学上产生的都是伪随机数,真正的随机数使用物理方法产生的。2、随机数种子:随机数的产生是由算术规则产生的......
  • 5月18号今日总结
    今日代码:%定义目标函数f=@(x)100*(x(1)^2-x(2))^2+(x(1)-1)^2;%定义目标函数的梯度grad_f=@(x)[400*x(1)*(x(1)^2-x(2))+2*(x(1)-1);-200*(x(1)^2-x(2))];%定义终止准则epsilon=1e-5;%定义最大迭代次数max_iterations=1000;%初始点......
  • 使用命令行方式program Intel FPGA程序
    命令行下载程序方式右键打开Windows终端(管理员),切换到待下载程序所在目录下,输入指令:quartus_pgm-cUSB-BlasterII-mjtag-o"bpv;XXX.pof",这里的XXX.pof就是待下载程序的完整名称-c代表指定对应的cable名称,如果不确定cable名称,在终端输入jtagconfig-n就可以查看到,如下:-m......
  • C++趣味编程
    分糖果1#include<iostream>2usingnamespacestd;3intmain()4{5inti,count=0;6inta[10]={10,2,8,22,16,4,10,6,14,20};7intb[10]={10,2,8,22,16,4,10,6,14,20};8do{9a[0]=b[0]/2+b[9]/2;10for(intj=1......
  • Keil_MDK中无法打开map文件的解决办法
    如果在MDK中打开map文件我们在STM32的开发过程中,经常会查看.map文件.map文件是MDK在编译过程中生成的一个包含镜像文件信息的重要文件,在程序编译后自动生成,比方这里我的工程下自动将.map文件生成到了与工程名同名文件夹下的Exe文件里面要想打开.map文件,直接在工程界面双击工程......
  • LLMs Fine-tuning 学习笔记(一):trl+peft
    目录1基本信息2实现步骤3代码分析1基本信息From:Fine-tuning20BLLMswithRLHFona24GBconsumerGPU(huggingface.co)Codes:trl/examples/sentiment/scripts/gpt-neox-20b_peftatmain·lvwerra/trl·GitHub相关工具:peft:用于微调大模型的python库官方文档:htt......
  • 渗透测试-struts2攻防环境搭建拿shell
    一、下载Jspstudy打开目录D:\JspStudy\tomcat\webapps二、打开struts2并进行拿shell1.打开struts2在浏览器中输入网址http://localhost:8080/struts2-showcase/showcase.action点击上面的Configuration,点击ActionChaining点击上面的Configuration,点击ActionChaining点击......