# package path
`import "path"`
path实现了对斜杠分隔的路径的实用操作函数。
## Index
* [Variables](#pkg-variables)
* [func IsAbs(path string) bool](#IsAbs)
* [func Split(path string) (dir, file string)](#Split)
* [func Join(elem ...string) string](#Join)
* [func Dir(path string) string](#Dir)
* [func Base(path string) string](#Base)
* [func Ext(path string) string](#Ext)
* [func Clean(path string) string](#Clean)
* [func Match(pattern, name string) (matched bool, err error)](#Match)
### Examples
* [Base](#example-Base)
* [Clean](#example-Clean)
* [Dir](#example-Dir)
* [Ext](#example-Ext)
* [IsAbs](#example-IsAbs)
* [Join](#example-Join)
* [Split](#example-Split)
## Variables
```
var ErrBadPattern = errors.New("syntax error in pattern")
```
ErrBadPattern表示一个glob模式匹配字符串的格式错误。
## func [IsAbs](https://github.com/golang/go/blob/master/src/path/path.go#L196 "View Source")
```
func IsAbs(path string) bool
```
IsAbs返回路径是否是一个绝对路径。
Example
```
fmt.Println(path.IsAbs("/dev/null"))
```
Output:
```
true
```
## func [Split](https://github.com/golang/go/blob/master/src/path/path.go#L142 "View Source")
```
func Split(path string) (dir, file string)
```
Split函数将路径从最后一个斜杠后面位置分隔为两个部分(dir和file)并返回。如果路径中没有斜杠,函数返回值dir会设为空字符串,file会设为path。两个返回值满足path == dir+file。
Example
```
fmt.Println(path.Split("static/myfile.css"))
```
Output:
```
static/ myfile.css
```
## func [Join](https://github.com/golang/go/blob/master/src/path/path.go#L150 "View Source")
```
func Join(elem ...string) string
```
Join函数可以将任意数量的路径元素放入一个单一路径里,会根据需要添加斜杠。结果是经过简化的,所有的空字符串元素会被忽略。
Example
```
fmt.Println(path.Join("a", "b", "c"))
```
Output:
```
a/b/c
```
## func [Dir](https://github.com/golang/go/blob/master/src/path/path.go#L207 "View Source")
```
func Dir(path string) string
```
Dir返回路径除去最后一个路径元素的部分,即该路径最后一个元素所在的目录。在使用Split去掉最后一个元素后,会简化路径并去掉末尾的斜杠。如果路径是空字符串,会返回".";如果路径由1到多个斜杠后跟0到多个非斜杠字符组成,会返回"/";其他任何情况下都不会返回以斜杠结尾的路径。
Example
```
fmt.Println(path.Dir("/a/b/c"))
```
Output:
```
/a/b
```
## func [Base](https://github.com/golang/go/blob/master/src/path/path.go#L176 "View Source")
```
func Base(path string) string
```
Base函数返回路径的最后一个元素。在提取元素前会求掉末尾的斜杠。如果路径是"",会返回".";如果路径是只有一个斜杆构成,会返回"/"。
Example
```
fmt.Println(path.Base("/a/b"))
```
Output:
```
b
```
## func [Ext](https://github.com/golang/go/blob/master/src/path/path.go#L163 "View Source")
```
func Ext(path string) string
```
Ext函数返回path文件扩展名。返回值是路径最后一个斜杠分隔出的路径元素的最后一个'.'起始的后缀(包括'.')。如果该元素没有'.'会返回空字符串。
Example
```
fmt.Println(path.Ext("/a/b/c/bar.css"))
```
Output:
```
.css
```
## func [Clean](https://github.com/golang/go/blob/master/src/path/path.go#L69 "View Source")
```
func Clean(path string) string
```
Clean函数通过单纯的词法操作返回和path代表同一地址的最短路径。
它会不断的依次应用如下的规则,直到不能再进行任何处理:
```
1\. 将连续的多个斜杠替换为单个斜杠
2\. 剔除每一个.路径名元素(代表当前目录)
3\. 剔除每一个路径内的..路径名元素(代表父目录)和它前面的非..路径名元素
4\. 剔除开始一个根路径的..路径名元素,即将路径开始处的"/.."替换为"/"
```
只有路径代表根地址"/"时才会以斜杠结尾。如果处理的结果是空字符串,Clean会返回"."。
参见[http://plan9.bell-labs.com/sys/doc/lexnames.html](http://plan9.bell-labs.com/sys/doc/lexnames.html)
Example
```
paths := []string{
"a/c",
"a//c",
"a/c/.",
"a/c/b/..",
"/../a/c",
"/../a/b/../././/c",
}
for _, p := range paths {
fmt.Printf("Clean(%q) = %q\n", p, path.Clean(p))
}
```
Output:
```
Clean("a/c") = "a/c"
Clean("a//c") = "a/c"
Clean("a/c/.") = "a/c"
Clean("a/c/b/..") = "a/c"
Clean("/../a/c") = "/a/c"
Clean("/../a/b/../././/c") = "/a/c"
```
## func [Match](https://github.com/golang/go/blob/master/src/path/match.go#L38 "View Source")
```
func Match(pattern, name string) (matched bool, err error)
```
如果name匹配shell文件名模式匹配字符串,Match函数返回真。该模式匹配字符串语法为:
```
pattern:
{ term }
term:
'*' 匹配0或多个非/的字符
'?' 匹配1个非/的字符
'[' [ '^' ] { character-range } ']' 字符组(必须非空)
c 匹配字符c(c != '*', '?', '\\', '[')
'\\' c 匹配字符c
character-range:
c 匹配字符c(c != '\\', '-', ']')
'\\' c 匹配字符c
lo '-' hi 匹配区间[lo, hi]内的字符
```
Match要求匹配整个name字符串,而不是它的一部分。只有pattern语法错误时,会返回ErrBadPattern。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe