golang的filepath包的几个函数的细微区别

简介: Clean(),Dir(),ABS()配合Walk()使用的时候,由于前三个函数返回值的细微差别,会造成遍历目录的时候,得到的结果不一样.filepath.Abs("./myDoc")//返回所给路径的绝对路径这时候遍历没有问题,2019/06/12 10:50:31 监控 : 1, D:\wo...

Clean(),Dir(),ABS()配合Walk()使用的时候,由于前三个函数返回值的细微差别,会造成遍历目录的时候,得到的结果不一样.
filepath.Abs("./myDoc")
//返回所给路径的绝对路径
这时候遍历没有问题,

2019/06/12 10:50:31 监控 : 1, D:\workspace\go-wikitten\src\myDoc
2019/06/12 10:50:31 文件 : 2, myDoc\.gitkeep
2019/06/12 10:50:31 文件 : 3, myDoc\Sample HTML document.html
2019/06/12 10:50:31 文件 : 4, myDoc\Sample Markdown document.md
2019/06/12 10:50:31 监控 : 5, D:\workspace\go-wikitten\src\myDoc\code snippets (expand me!)
2019/06/12 10:50:31 文件 : 6, myDoc\code snippets (expand me!)\Bash.sh
2019/06/12 10:50:31 文件 : 7, myDoc\code snippets (expand me!)\CSS.css
2019/06/12 10:50:31 文件 : 8, myDoc\code snippets (expand me!)\JavaScript.js
2019/06/12 10:50:31 文件 : 9, myDoc\code snippets (expand me!)\PHP.php
2019/06/12 10:50:31 文件 : 10, myDoc\code snippets (expand me!)\Python.py
2019/06/12 10:50:31 文件 : 11, myDoc\code snippets (expand me!)\Ruby.rb
2019/06/12 10:50:31 文件 : 12, myDoc\code snippets (expand me!)\SQL.sql
2019/06/12 10:50:31 文件 : 13, myDoc\code snippets (expand me!)\Scheme.scm
2019/06/12 10:50:31 文件 : 14, myDoc\code snippets (expand me!)\XML.xml
2019/06/12 10:50:31 文件 : 15, myDoc\index.md
2019/06/12 10:50:31 监控 : 16, D:\workspace\go-wikitten\src\myDoc\you can also
2019/06/12 10:50:31 监控 : 17, D:\workspace\go-wikitten\src\myDoc\you can also\nest directories
2019/06/12 10:50:31 文件 : 18, myDoc\you can also\nest directories\binary files are OK too.jpg

path.Clean("./myDoc"));
//返回等价的最短路径
//1.用一个斜线替换多个斜线
//2.清除当前路径.
//3.清除内部的..和他前面的元素
//4.以/..开头的,变成/
由于我的目录不存在1,3,4的情况,所以遍历也是符合我的期望,只是把绝对路径换成了相对路径

2019/06/12 10:56:18 监控 : 1, myDoc
2019/06/12 10:56:18 文件 : 2, myDoc\.gitkeep
2019/06/12 10:56:18 文件 : 3, myDoc\Sample HTML document.html
2019/06/12 10:56:18 文件 : 4, myDoc\Sample Markdown document.md
2019/06/12 10:56:18 监控 : 5, myDoc\code snippets (expand me!)
2019/06/12 10:56:18 文件 : 6, myDoc\code snippets (expand me!)\Bash.sh
2019/06/12 10:56:18 文件 : 7, myDoc\code snippets (expand me!)\CSS.css
2019/06/12 10:56:18 文件 : 8, myDoc\code snippets (expand me!)\JavaScript.js
2019/06/12 10:56:18 文件 : 9, myDoc\code snippets (expand me!)\PHP.php
2019/06/12 10:56:18 文件 : 10, myDoc\code snippets (expand me!)\Python.py
2019/06/12 10:56:18 文件 : 11, myDoc\code snippets (expand me!)\Ruby.rb
2019/06/12 10:56:18 文件 : 12, myDoc\code snippets (expand me!)\SQL.sql
2019/06/12 10:56:18 文件 : 13, myDoc\code snippets (expand me!)\Scheme.scm
2019/06/12 10:56:18 文件 : 14, myDoc\code snippets (expand me!)\XML.xml
2019/06/12 10:56:18 文件 : 15, myDoc\index.md
2019/06/12 10:56:18 监控 : 16, myDoc\you can also
2019/06/12 10:56:18 监控 : 17, myDoc\you can also\nest directories
2019/06/12 10:56:18 文件 : 18, myDoc\you can also\nest directories\binary files are OK too.jpg

path.Dir("./myDoc"));
//返回路径最后一个元素的目录
//路径为空则返回.
这个遍历,刚开始的时候,我以为是正确的,后面才发现不对了.
首先,额外监控了给定目录的父目录,也就是myDoc的父目录,参见第一行输出
其次,如果目录下没有文件,接着还是目录,参见输出的17和18行,遍历的时候跳过去了

2019/06/12 10:48:30 监控 : 1, .
2019/06/12 10:48:30 文件 : 2, myDoc\.gitkeep
2019/06/12 10:48:30 文件 : 3, myDoc\Sample HTML document.html
2019/06/12 10:48:30 文件 : 4, myDoc\Sample Markdown document.md
2019/06/12 10:48:30 监控 : 5, myDoc
2019/06/12 10:48:30 文件 : 6, myDoc\code snippets (expand me!)\Bash.sh
2019/06/12 10:48:30 文件 : 7, myDoc\code snippets (expand me!)\CSS.css
2019/06/12 10:48:30 文件 : 8, myDoc\code snippets (expand me!)\JavaScript.js
2019/06/12 10:48:30 文件 : 9, myDoc\code snippets (expand me!)\PHP.php
2019/06/12 10:48:30 文件 : 10, myDoc\code snippets (expand me!)\Python.py
2019/06/12 10:48:30 文件 : 11, myDoc\code snippets (expand me!)\Ruby.rb
2019/06/12 10:48:30 文件 : 12, myDoc\code snippets (expand me!)\SQL.sql
2019/06/12 10:48:30 文件 : 13, myDoc\code snippets (expand me!)\Scheme.scm
2019/06/12 10:48:30 文件 : 14, myDoc\code snippets (expand me!)\XML.xml
2019/06/12 10:48:30 文件 : 15, myDoc\index.md
2019/06/12 10:48:30 监控 : 16, myDoc
2019/06/12 10:48:30 监控 : 17, myDoc\you can also
2019/06/12 10:48:30 文件 : 18, myDoc\you can also\nest directories\binary files are OK too.jpg
目录
相关文章
|
17天前
|
Go
Golang的math包常用方法
这篇文章介绍了Golang的math包中的常量和常用方法,并通过示例代码展示了如何使用这些常量和方法。
162 87
Golang的math包常用方法
|
15天前
|
Go
Golang语言之函数(func)进阶篇
这篇文章是关于Golang语言中函数高级用法的教程,涵盖了初始化函数、匿名函数、闭包函数、高阶函数、defer关键字以及系统函数的使用和案例。
16 3
Golang语言之函数(func)进阶篇
|
15天前
|
Go
Golang语言之函数(func)基础篇
这篇文章深入讲解了Golang语言中函数的定义和使用,包括函数的引入原因、使用细节、定义语法,并通过多个案例展示了如何定义不返回任何参数、返回一个或多个参数、返回值命名、可变参数的函数,同时探讨了函数默认值传递、指针传递、函数作为变量和参数、自定义数据类型以及返回值为切片类型的函数。
18 2
Golang语言之函数(func)基础篇
|
15天前
|
Unix Go
Golang语言标准库time之日期和时间相关函数
这篇文章是关于Go语言日期和时间处理的文章,介绍了如何使用Go标准库中的time包来处理日期和时间。
29 3
|
15天前
|
存储 Go
Golang语言基于go module方式管理包(package)
这篇文章详细介绍了Golang语言中基于go module方式管理包(package)的方法,包括Go Modules的发展历史、go module的介绍、常用命令和操作步骤,并通过代码示例展示了如何初始化项目、引入第三方包、组织代码结构以及运行测试。
18 3
|
15天前
|
Go
Golang语言基于GOPATH方式管理包(package)
这篇文章详细介绍了Golang语言中基于GOPATH方式管理包(package)的方法,包括包的概述、定义、引入格式、别名使用、匿名引入,以及如何快速入门自定义包,并通过具体代码案例展示了包的环境准备、代码编写、细节说明和程序运行。
27 3
|
15天前
|
Go
Golang语言之包依赖管理
这篇文章详细介绍了Go语言的包依赖管理工具,包括godep和go module的使用,以及如何在项目中使用go module进行依赖管理,还探讨了如何导入本地包和第三方库下载的软件包存放位置。
16 3
|
19天前
|
Go 开发者
|
15天前
|
Go
Golang语言之管道channel快速入门篇
这篇文章是关于Go语言中管道(channel)的快速入门教程,涵盖了管道的基本使用、有缓冲和无缓冲管道的区别、管道的关闭、遍历、协程和管道的协同工作、单向通道的使用以及select多路复用的详细案例和解释。
47 4
Golang语言之管道channel快速入门篇
|
15天前
|
Go
Golang语言文件操作快速入门篇
这篇文章是关于Go语言文件操作快速入门的教程,涵盖了文件的读取、写入、复制操作以及使用标准库中的ioutil、bufio、os等包进行文件操作的详细案例。
38 4
Golang语言文件操作快速入门篇