go语言学习-文件操作 path path/filepath
2016-07-27 00:00
686 查看
摘要: 文件的操作,路径,读写等。 path包 path/filepath讲解等
Windos 中的路径格式 为C:\Windows路径中的分隔符是\。
而**在Go中,只认'/' **。
2.func Clean(path string) string 返回一个跟 path 等价的短路径。一般在路径中出现./或../时 可以使用本函数,返回一个等价的路径
3.func Dir(path string) string 返回路径中的目录部分。也就是最后一个/前面的部分。
4.func Ext(path string) string 用来取文件的扩展名。
5.func IsAbs(path string) bool 用来判断路径是否绝对路径。在 Linux 下如果路径是以/开头 的是绝对路径,如/user/bin,否则是相对路径;Windows 下,以盘符开头的是绝对路径,如 C:\Windows\system,而 Windows\system 是相对路径
6.func Join(elem ...string) string,用来进行路径的连接。如将 a/b,和 c 连接成 a/b/c。
7.func Split(path string) (dir, file string)把路径分割成目录和文件两部分。
2.func Walk(root string, walkFn WalkFunc) error,用来遍历 root 目录下的所有文件和子目录。
文件操作
在 Linux 中,路径的格式为/user/bin 路径中的分隔符是/;Windos 中的路径格式 为C:\Windows路径中的分隔符是\。
而**在Go中,只认'/' **。
path 路径
1.func Base(path string) string 返回路径的最后一部分。fmt.Println(path.Base("/usr/bin")) //bin fmt.Println(path.Base("")) //输出. fmt.Println(path.Base("C:\\Windows"))/*无法识别Windows下的路径分隔符, 将会把C:\\Windows做为一个路径 * / fmt.Println(path.Base(strings.Replace("C:\\Windows", "\\", "/", -1))) /*把\转换成/*/
2.func Clean(path string) string 返回一个跟 path 等价的短路径。一般在路径中出现./或../时 可以使用本函数,返回一个等价的路径
fmt.Println(path.Clean("/a/b/../c")) /*/a/c*/ fmt.Println(path.Clean("/a/b/../././c")) /*/a/c*/
3.func Dir(path string) string 返回路径中的目录部分。也就是最后一个/前面的部分。
fmt.Println(path.Dir("/a/b/../c/d/e")) /*/a/c/d*/ fmt.Println(path.Dir("/a/b/")) /*/a/b*/
4.func Ext(path string) string 用来取文件的扩展名。
fmt.Println(path.Ext("/a/b/../c/d./e")) /*没有扩展名*/ fmt.Println(path.Ext("/a/b/test.txt")) /*.txt*/
5.func IsAbs(path string) bool 用来判断路径是否绝对路径。在 Linux 下如果路径是以/开头 的是绝对路径,如/user/bin,否则是相对路径;Windows 下,以盘符开头的是绝对路径,如 C:\Windows\system,而 Windows\system 是相对路径
fmt.Println(path.IsAbs("/a/b/c")) fmt.Println(path.IsAbs(strings.Replace("C:\\Windows\\system", "\\", "/", -1))) /*Go 只识别/所以需要转换一下*/ 第一个输出 true,/a/b/c 是绝对路径;第二个输出 false. C:\Windows\system 是绝对路径,但 Go 好像只支持 Linux 格式的路径,所以此处判断错误
6.func Join(elem ...string) string,用来进行路径的连接。如将 a/b,和 c 连接成 a/b/c。
fmt.Println(path.Join("a/b", "c"))/*a/b/c*/ fmt.Println(path.Join("C:\\Windows", "System"))/*C:\Windows/System*/
7.func Split(path string) (dir, file string)把路径分割成目录和文件两部分。
fmt.Println(path.Split("/a/b/test.txt")) /*/a/b/ test.txt*/ fmt.Println(path.Split("/a/b/c/")) /*/a/b/c/ */ fmt.Println(path.Split("/a/b/c")) /*/a/b/ c */
filepath
1.func Abs(path string) (string, error)用来把相对路径转换成绝对路径,该函数位于 path/filepath 包中fmt.Println(filepath.Abs(".")) /*当前文件所在目录*/
2.func Walk(root string, walkFn WalkFunc) error,用来遍历 root 目录下的所有文件和子目录。
//WalkFunc 是一个函数类型,定为 type WalkFunc func(path string, info os.FileInfo, err error) error,path 为当前文件或文件侠的完整路径,info 是 os.FileInfo 结构的表示 package main import ( "fmt" "os" "path/filepath" ) func DispFile(path string, info os.FileInfo, err error) error { fmt.Println(path, "-------", info.Name(), "------", info.IsDir()) return nil } func main() { filepath.Walk(".", DispFile) }
相关文章推荐
- ubuntu logout commend
- django 后台 链接图片URL报错
- django 后台输入中文,保存时乱码
- HDU1847 Good Luck in CET-4 Everybody!
- 蒙特卡罗树搜索+深度学习 -- AlphaGo原版论文阅读笔记
- GOF 23种设计模式 怎么分类
- django学习笔记(一):环境搭建
- Google Chrome 中安装 PostMan 扩展
- 关于slice的一点思考
- HDU 3363 Ice-sugar Gourd
- HDU 3363 Ice-sugar Gourd (滑动窗口)
- HDU 3363 Ice-sugar Gourd (贪心)
- Mac上安装go环境
- Google推荐的图片加载库Glide
- 从零开始带你认识最新的图片格式WEBP,浓缩的精华!
- GO语言高并发学习心得体会
- GO语言Comma-ok断言
- Google发布了能理解人类语言的云服务
- 白菜之compareTo()和compareToIgnoreCase()
- HDU 3363 Ice-sugar Gourd