23 September 2017

Variables

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

function

func Base(path string) string

fmt.Println(path.Base("/a/b"))  // b
fmt.Println(path.Base("/"))     // /
fmt.Println(path.Base(""))      // .

func Clean(path string) string

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))
}

// 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"
// Clean("")                    = "."

func Dir(path string) string

fmt.Println(path.Dir("/a/b/c")) // /a/b
fmt.Println(path.Dir("a/b/c"))  // a/b
fmt.Println(path.Dir("/"))      // /
fmt.Println(path.Dir(""))       // .

func Ext(path string) string

fmt.Println(path.Ext("/a/b/c/bar.css")) // .css
fmt.Println(path.Ext("/"))              //
fmt.Println(path.Ext(""))               //

func IsAbs(path string) bool

fmt.Println(path.IsAbs("/dev/null"))  // true
fmt.Println(path.IsAbs("./dev/null")) // false

func Join(elem …string) string

fmt.Println(path.Join("a", "b", "c")) // a/b/c
fmt.Println(path.Join("a", "b/c"))    // a/b/c
fmt.Println(path.Join("a/b", "c"))    // a/b/c
fmt.Println(path.Join("", ""))             //
fmt.Println(path.Join("a", ""))            // a
fmt.Println(path.Join("", "a"))            // a

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

var b bool

b, _ = path.Match("*", "/home/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/*", "/home/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/*/*", "/home/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/[a-z]/*", "/xyz/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/[a-z]/*", "/x/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/[a-z]*/*", "/xyz/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/\\*/*", "/\\xyz/user") // \\xyz 这里把 \\ 当成两个 '\'
fmt.Printf("%v\n", b)                      // false

b, _ = path.Match("/\\\\*/*", "/\\xyz/user") // \\xyz 这里把 \\ 当成两个 '\'
fmt.Printf("%v\n", b)                        // true

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

var dir string
var file string

dir, file = path.Split("/usr/local//go")
fmt.Printf("DIR: |%v|, FILE: |%v|\n", dir, file) // |/usr/local//|, |go|

dir, file = path.Split("static/myfile.css")
fmt.Printf("DIR: |%v|, FILE: |%v|\n", dir, file) // |static/|, |myfile.css|

dir, file = path.Split("myfile.css")
fmt.Printf("DIR: |%v|, FILE: |%v|\n", dir, file) // ||, |myfile.css|

dir, file = path.Split("")
fmt.Printf("DIR: |%v|, FILE: |%v|\n", dir, file) // ||, ||

filepath

Constants

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

Variables

var ErrBadPattern = errors.New("syntax error in pattern")
var SkipDir = errors.New("skip this directory")

func Abs(path string) (string, error)

var s string

s, _ = filepath.Abs("/root/")
fmt.Printf("%v\n", s)   // /root

s, _ = filepath.Abs("root/")
fmt.Printf("%v\n", s)   // /Users/user/proj/go/tour/root

func Base(path string) string

var s string

s = filepath.Base("/root/path")
fmt.Printf("%v\n", s) // path

s = filepath.Base("root/")
fmt.Printf("%v\n", s) // root

s = filepath.Base("root/file.txt")
fmt.Printf("%v\n", s) // file.txt

func Clean(path string) string

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, filepath.Clean(p))
}

// 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"
// Clean("")                    = "."

func Dir(path string) string

fmt.Println(path.Dir("/a/b/c")) // /a/b
fmt.Println(path.Dir("a/b/c"))  // a/b
fmt.Println(path.Dir("/"))      // /
fmt.Println(path.Dir(""))       // .

func EvalSymlinks(path string) (string, error)

var s string

s, _ = filepath.EvalSymlinks("~/.emacs.d")
fmt.Printf("%s\n", s) //

s, _ = filepath.EvalSymlinks("/Users/user/.emacs.d")
fmt.Printf("%s\n", s) // /Users/user/etc/emacs.d

func Ext(path string) string

fmt.Println(path.Ext("/a/b/c/bar.css")) // .css
fmt.Println(path.Ext("/"))              //
fmt.Println(path.Ext(""))               //

func FromSlash(path string) string

FromSlash returns the result of replacing each slash ('/') character in path with a separator character. Multiple slashes are replaced by multiple separators.

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

m, _ := filepath.Glob("/Users/user/*")

fmt.Printf("%v\n", m)

func HasPrefix(p, prefix string) bool

m := filepath.HasPrefix("/Users/user/", "/U")
fmt.Printf("%v\n", m)  // true

func IsAbs(path string) bool

m := filepath.IsAbs("/Users/user/")
fmt.Printf("%v\n", m)   // true

m := filepath.IsAbs("Users/user/")
fmt.Printf("%v\n", m)   // fasle

func Join(elem …string) string

fmt.Println(filepath.Join("a", "b", "c")) // a/b/c
fmt.Println(filepath.Join("a", "b/c"))    // a/b/c
fmt.Println(filepath.Join("a/b", "c"))    // a/b/c
fmt.Println(filepath.Join("a/b", "/c"))   // a/b/c

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

var b bool

b, _ = path.Match("*", "/home/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/*", "/home/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/*/*", "/home/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/[a-z]/*", "/xyz/user")
fmt.Printf("%v\n", b) // false

b, _ = path.Match("/[a-z]/*", "/x/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/[a-z]*/*", "/xyz/user")
fmt.Printf("%v\n", b) // true

b, _ = path.Match("/\\*/*", "/\\xyz/user") // \\xyz 这里把 \\ 当成两个 '\'
fmt.Printf("%v\n", b)                      // false

b, _ = path.Match("/\\\\*/*", "/\\xyz/user") // \\xyz 这里把 \\ 当成两个 '\'
fmt.Printf("%v\n", b)                        // true

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

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

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

// /a/b/c: b/c <nil>
// /b/c: ../b/c <nil>
// ./b/c:  Rel: can't make ./b/c relative to /a

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

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

// input: "/home/arnie/amelia.jpg"
// dir: "/home/arnie/"
// file: "amelia.jpg"
// input: "/mnt/photos/"
// dir: "/mnt/photos/"
// file: ""
// input: "rabbit.jpg"
// dir: ""
// file: "rabbit.jpg"
// input: "/usr/local//go"
// dir: "/usr/local//"
// file: "go"

func SplitList(path string) []string

fmt.Println(filepath.SplitList("/a/b/c:/usr/bin"))  // [/a/b/c /usr/bin]

func ToSlash(path string) string

ToSlash returns the result of replacing each separator character in path with a slash ('/') character. Multiple separators are replaced by multiple slashes.

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 "".

func Walk(root string, walkFn WalkFunc) error

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

f := func(path string, info os.FileInfo, err error) error {
     fmt.Printf("%v\n", path)
     fmt.Printf("%v\n", info)
     fmt.Printf("%v\n", err)

     return nil
}

filepath.Walk("/xxx", f)