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
目录
相关文章
|
1月前
|
Unix 程序员 编译器
第六章 Golang函数
第六章 Golang函数
28 0
|
3月前
|
编译器 Go
Golang底层原理剖析之函数调用栈-传参和返回值
Golang底层原理剖析之函数调用栈-传参和返回值
19 0
|
3月前
|
存储 编译器 Go
Golang底层原理剖析之函数调用栈-栈帧布局与函数跳转
Golang底层原理剖析之函数调用栈-栈帧布局与函数跳转
33 0
|
5月前
|
监控 网络协议 Go
Golang抓包:实现网络数据包捕获与分析
Golang抓包:实现网络数据包捕获与分析
|
6月前
|
负载均衡 中间件 Go
Golang 微服务工具包 Go kit
Golang 微服务工具包 Go kit
43 0
|
21小时前
|
Go
Golang深入浅出之-Go语言函数基础:定义、调用与多返回值
【4月更文挑战第21天】Go语言函数是代码组织的基本单元,用于封装可重用逻辑。本文介绍了函数定义(包括基本形式、命名、参数列表和多返回值)、调用以及匿名函数与闭包。在函数定义时,注意参数命名和注释,避免参数顺序混淆。在调用时,要检查并处理多返回值中的错误。理解闭包原理,小心处理外部变量引用,以提升代码质量和可维护性。通过实践和示例,能更好地掌握Go语言函数。
9 1
Golang深入浅出之-Go语言函数基础:定义、调用与多返回值
|
3月前
|
Go
golang力扣leetcode 396.旋转函数
golang力扣leetcode 396.旋转函数
22 1
|
3月前
|
Go
Golang内置Log包的基本使用
Golang内置Log包的基本使用
25 0
|
4月前
|
JSON Cloud Native 网络协议
golang validator 包的使用指北
golang validator 包的使用指北
|
4月前
|
Cloud Native Go C语言
golang flag 包的使用指北
golang flag 包的使用指北