非常教程

Go参考手册

路径 | path

文件路径 | path/filepath

  • import "path/filepath"
  • 概述
  • 索引
  • 例子

概述

软件包文件路径以与目标操作系统定义的文件路径兼容的方式实现用于操作文件名路径的实用程序例程。

filepath 包使用正斜杠或反斜杠,具体取决于操作系统。要处理不管操作系统如何都始终使用正斜杠的 URL,请参阅路径包。

索引

  • 常量
  • Variables
  • func Abs(path string) (string, error)
  • func Base(path string) string
  • func Clean(path string) string
  • func Dir(path string) string
  • func EvalSymlinks(path string) (string, error)
  • func Ext(path string) string
  • func FromSlash(path string) string
  • func Glob(pattern string) (matches []string, err error)
  • func HasPrefix(p, prefix string) bool
  • func IsAbs(path string) bool
  • func Join(elem ...string) string
  • func Match(pattern, name string) (matched bool, err error)
  • func Rel(basepath, targpath string) (string, error)
  • func Split(path string) (dir, file string)
  • func SplitList(path string) []string
  • func ToSlash(path string) string
  • func VolumeName(path string) string
  • func Walk(root string, walkFn WalkFunc) error
  • type WalkFunc

例子

Join Rel Split SplitList

包文件

match.go path.go path_unix.go symlink.go symlink_unix.go

常量

const (
        Separator     = os.PathSeparator
        ListSeparator = os.PathListSeparator
)

变量

ErrBadPattern 表示通配模式格式错误。

var ErrBadPattern = errors.New("syntax error in pattern")

SkipDir 被用作来自 WalkFuncs 的返回值,以指示呼叫中指定的目录将被跳过。它不会被任何函数返回为错误。

var SkipDir = errors.New("skip this directory")

func Abs(显示源文件)

func Abs(path string) (string, error)

Abs 返回路径的绝对表示。如果路径不是绝对路径,它将与当前工作目录连接,将其变为绝对路径。并不保证给定文件的绝对路径名是唯一的。Abs 在结果上调用 Clean 。

func Base(显示源文件)

func Base(path string) string

Base 返回路径的最后一个元素。在提取最后一个元素之前,删除了尾部路径分隔符。如果路径为空,则 Base 返回“。”。如果路径完全由分隔符组成,则 Base 将返回一个分隔符。

func Clean(显示源文件)

func Clean(path string) string

Clean 通过纯词法处理返回等价于路径的最短路径名。它反复应用以下规则,直到不能进行进一步的处理:

1. Replace multiple Separator elements with a single one.
2. Eliminate each . path name element (the current directory).
3. Eliminate each inner .. path name element (the parent directory)
   along with the non-.. element that precedes it.
4. Eliminate .. elements that begin a rooted path:
   that is, replace "/.." by "/" at the beginning of a path,
   assuming Separator is '/'.

仅当返回的路径表示根目录(例如 Unix 或C:\Windows 上的“/”)时,才以斜线结尾。

最后,任何出现的斜杠被分隔符替换。

如果此进程的结果是空字符串,则 Clean 将返回字符串“。”。

另请参阅 Rob Pike,“计划9中的词汇文件名或正确获取Dot-Dot”,https://9p.io/sys/doc/lexnames.html

func Dir(显示源文件)

func Dir(path string) string

Dir 返回路径的最后一个元素,通常是路径的目录。删除最后一个元素后,Dir 在路径上调用 Clean 并删除尾部斜线。如果路径为空,则 Dir 返回“。”。如果路径完全由分隔符组成,则 Dir 返回一个分隔符。返回的路径不会以分隔符结尾,除非它是根目录。

func EvalSymlinks(显示源文件)

func EvalSymlinks(path string) (string, error)

EvalSymlinks 在评估任何符号链接后返回路径名称。如果路径是相对的,则结果将与当前目录相关,除非其中一个组件是绝对符号链接。EvalSymlinks 在结果上调用 Clean 。

func Ext(显示源文件)

func Ext(path string) string

Ext 返回 path 使用的文件扩展名。扩展名是从路径最后一个元素的最后一个点开始的后缀; 如果没有点,它是空的。

func FromSlash(显示源文件)

func FromSlash(path string) string

FromSlash 返回用分隔符替换路径中每个斜杠('/')字符的结果。多个斜杠被多个分隔符替代。

func Glob(显示源文件)

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

如果没有匹配的文件,Glob 返回匹配模式的所有文件的名称或nil。模式的语法与 Match 中的相同。该模式可以描述分层名称,例如 / usr / * / bin / ed(假设分隔符是'/')。

Glob 忽略文件系统错误,例如读取目录的 I/O 错误。当模式格式错误时,唯一可能返回的错误是 ErrBadPattern 。

func HasPrefix(显示源文件)

func HasPrefix(p, prefix string) bool

HasPrefix 存在历史兼容性,不应使用。

弃用:HasPrefix 不遵守路径边界,并且在需要时不会忽略大小写。

func IsAbs(显示源文件)

func IsAbs(path string) bool

IsAbs 报告路径是否绝对。

func Join(显示源文件)

func Join(elem ...string) string

Join 将任意数量的路径元素连接到单个路径中,如有必要添加分隔符。加入调用清理结果; 特别是,所有空串都被忽略。在 Windows 上,当且仅当第一个路径元素是 UNC 路径时,结果为 UNC 路径。

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println("On Unix:")
	fmt.Println(filepath.Join("a", "b", "c"))
	fmt.Println(filepath.Join("a", "b/c"))
	fmt.Println(filepath.Join("a/b", "c"))
	fmt.Println(filepath.Join("a/b", "/c"))
}

func Match(显示源文件)

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

匹配报告名称是否与 shell 文件名称模式相匹配。模式语法是:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-Separator characters
	'?'         matches any single non-Separator character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

匹配需要匹配所有名称的模式,而不仅仅是一个子字符串。当模式格式错误时,唯一可能返回的错误是 ErrBadPattern 。

在 Windows 上,转义被禁用。相反,'\'被视为路径分隔符。

func Rel(显示源文件)

func Rel(basepath, targpath string) (string, error)

当通过介入分隔符连接到 basepath 时,Rel 返回一个与 targpath 词法相同的相对路径。也就是说,Join(basepath,Rel(basepath,targpath))等同于 targpath 本身。成功时,即使 basepath 和 targpath 不共享任何元素,返回的路径也总是相对于 basepath 。如果无法相对于基本路径创建 targpath,或者如果知道当前工作目录来计算它,则会返回错误。Rel 调用清理结果。

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	paths := []string{
		"/a/b/c",
		"/b/c",
		"./b/c",
	}
	base := "/a"

	fmt.Println("On Unix:")
	for _, p := range paths {
		rel, err := filepath.Rel(base, p)
		fmt.Printf("%q: %q %v\n", p, rel, err)
	}

}

func Split(显示源文件)

func Split(path string) (dir, file string)

Split 在最后一个 Separator 之后立即拆分路径,将其分隔成一个目录和文件名组件。如果路径中没有分隔符,则 Split 将返回一个空的目录并将文件设置为路径。返回的值具有 path = dir + file 的属性。

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	paths := []string{
		"/home/arnie/amelia.jpg",
		"/mnt/photos/",
		"rabbit.jpg",
		"/usr/local//go",
	}
	fmt.Println("On Unix:")
	for _, p := range paths {
		dir, file := filepath.Split(p)
		fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file)
	}
}

func SplitList(显示源文件)

func SplitList(path string) []string

SplitList 拆分由特定于操作系统的 ListSeparator 连接的路径列表,通常在 PATH 或 GOPATH 环境变量中找到。与 strings.Split 不同,SplitList 在传递空字符串时返回空片段。

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin"))
}

func ToSlash(显示源文件)

func ToSlash(path string) string

ToSlash 返回用斜线('/')字符替换路径中每个分隔符的结果。多个分隔符被多个斜线替代。

func VolumeName(显示源文件)

func VolumeName(path string) string

VolumeName 返回领先的卷名称。给定“C:\ foo \ bar”,它会在 Windows 上返回“C:”。鉴于“\host\share\foo”它返回“\host\share”。在其他平台上,它返回“”。

func Walk(显示源文件)

func Walk(root string, walkFn WalkFunc) error

Walk 遍历以根为根的文件树,为树中的每个文件或目录(包括根)调用 walkFn 。所有访问文件和目录的错误都由 walkFn 过滤。这些文件按照词汇顺序走,这使得输出具有确定性,但意味着对于非常大的目录,Walk 可能效率低下。走并不遵循符号链接。

type WalkFunc(显示源文件)

WalkFunc 是 Walk 所访问的每个文件或目录所调用的函数的类型。path 参数包含 Walk 作为前缀的参数; 也就是说,如果使用包含文件“a”的目录“dir”调用 Walk,则会使用参数“dir / a”调用 walk 功能。info 参数是指定路径的 os.FileInfo 。

如果出现问题时走到由路径命名的文件或目录,传入的错误将描述问题,并且函数可以决定如何处理该错误(并且 Walk 不会进入该目录)。如果返回错误,则处理停止。唯一的例外是函数返回特殊值 SkipDir 。如果函数在目录上调用时返回 SkipDir,则 Walk 完全跳过目录的内容。如果函数在非目录文件上调用时返回 SkipDir,则 Walk 会跳过包含目录中的其余文件。

type WalkFunc func(path string, info os.FileInfo, err error) error

路径 | path相关

Go

Go 是一种编译型语言,它结合了解释型语言的游刃有余,动态类型语言的开发效率,以及静态类型的安全性。它也打算成为现代的,支持网络与多核计算的语言。要满足这些目标,需要解决一些语言上的问题:一个富有表达能力但轻量级的类型系统,并发与垃圾回收机制,严格的依赖规范等等。这些无法通过库或工具解决好,因此Go也就应运而生了。

主页 https://golang.org/
源码 https://go.googlesource.com/go
发布版本 1.9.2

Go目录

1.档案 | archive
2.缓冲区 | bufio
3.内置 | builtin
4.字节 | bytes
5.压缩 | compress
6.容器 | container
7.上下文 | context
8.加密 | crypto
9.数据库 | database
10.调试 | debug
11.编码 | encoding
12.错误 | errors
13. expvar
14.flag
15. fmt
16. go
17.散列 | hash
18.html
19.图像 | image
20.索引 | index
21.io
22.日志 | log
23.数学 | math
24. math/big
25.math/bits
26.math/cmplx
27.math/rand
28.拟态 | mime
29.net
30.net/http
31. net/mail
32. net/rpc
33.net/smtp
34. net/textproto
35. net/url
36.os
37.路径 | path
38.插件 | plugin
39.反射 | reflect
40.正则表达式 | regexp
41.运行时 | runtime
42.排序算法 | sort
43.转换 | strconv
44.字符串 | strings
45.同步 | sync
46.系统调用 | syscall
47.测试 | testing
48.文本 | text
49.时间戳 | time
50.unicode
51.不安全性 | unsafe
52.Go 语言数据类型
53.Go 语言基础语法
54.Go 语言结构
55.Go 语言 select 语句
56.Go 语言 switch 语句
57.Go 语言 if 语句嵌套
58.Go 语言 if…else 语句
59.Go 语言 if 语句
60.Go 语言运算符
61.Go 语言常量
62.Go 语言函数闭包
63.Go 语言函数作为实参
64.Go 语言函数引用传递值
65.Go 语言函数值传递值
66.Go 语言函数
67.Go 语言 goto 语句
68.Go 语言 continue 语句
69.Go 语言 break 语句
70.Go 语言循环嵌套
71.Go 语言 for 循环
72.Go 语言结构体
73.Go 语言指针作为函数参数
74.Go 语言指向指针的指针
75.Go 语言指针数组
76.Go 语言指针
77.Go 语言向函数传递数组
78.Go 语言多维数组
79.Go 语言变量作用域
80.Go 语言函数方法
81.Go 错误处理
82.Go 语言接口
83.Go 语言类型转换
84.Go 语言递归函数
85.Go 语言Map(集合)
86.Go 语言范围(Range)
87.Go 语言切片(Slice)
88.Go 并发
89.Go fmt.Sprintf 格式化字符串