目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • 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 将返回字符串“。”。

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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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
上一篇: 下一篇: