path

https://golang.org/pkg/path/

path 提供了斜线号路径处理,但仅仅支持“/“左斜线处理。跨平台,特别是windows路径和盘符处理,请使用filepath。

func Base

1
func Base(path string) string

返回最后一个路径元素。空返回”.”, 空路径”/“返回”/“。

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
"fmt"
"path"
)

func main() {
fmt.Println(path.Base("/a/b"))
fmt.Println(path.Base("/"))
fmt.Println(path.Base(""))
}

结果:

1
2
3
b
/
.

func Dir

1
func Dir(path string) string

返回除了最后一个元素的所有路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import (
"fmt"
"path"
)

func main() {
fmt.Println(path.Dir("/a/b/c"))
fmt.Println(path.Dir("a/b/c"))
fmt.Println(path.Dir("/"))
fmt.Println(path.Dir(""))
}

结果

1
2
3
4
/a/b
a/b
/
.

func Ext

1
func Ext(path string) string

Ext返回文件后缀,如果不存在返回空 -> “”

func Join

1
func Join(elem ...string) string

Join,链接,结果用调用Clean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
"fmt"
"path"
)

func main() {
fmt.Println(path.Join("a", "b", "c"))
fmt.Println(path.Join("a", "b/c"))
fmt.Println(path.Join("a/b", "c"))
fmt.Println(path.Join("", ""))
fmt.Println(path.Join("a", ""))
fmt.Println(path.Join("", "a"))
}
1
2
3
4
5
6
a/b/c
a/b/c
a/b/c

a
a

func Match

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

通配符匹配,规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pattern:
{ term }
term:
'*' matches any sequence of non-/ characters
'?' matches any single non-/ 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错误。

func Split

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

Split 分割为dir和file

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
"fmt"
"path"
)

func main() {
fmt.Println(path.Split("static/myfile.css"))
fmt.Println(path.Split("myfile.css"))
fmt.Println(path.Split(""))
}

1
2
static/ myfile.css
myfile.css

func Clean

清洗整理函数,看规则:

  1. Replace multiple slashes with a single slash.
  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.

filepath

https://golang.org/pkg/path/filepath/
除了path包中的func,额外提供了跨平台支持,特别是win支持,盘符的支持。

func Abs

1
func Abs(path string) (string, error)

返回绝对路径

func Glob

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

Match类似,都是通配符匹配

func HasPrefix

1
func HasPrefix(p, prefix string) bool

不推荐使用

func IsAbs

1
func IsAbs(path string) bool

func Rel

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

基于basepath返回tragpath的相对路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
}

}
1
2
3
4
On Unix:
"/a/b/c": "b/c" <nil>
"/b/c": "../b/c" <nil>
"./b/c": "" Rel: can't make ./b/c relative to /a

func VolumeName

1
func VolumeName(path string) string

VolumeName returns leading volume name. Given “C:\foo\bar” it returns “C:” on Windows. Given “\host\share\foo” it returns “\host\share”. On other platforms it returns “”.

转载请注明来源,https://blog.vicyu.com
由于水平有限,行文难免出错,恳请读者批评指正。