github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/path/filepath/example_unix_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !windows,!plan9 6 7 package filepath_test 8 9 import ( 10 "fmt" 11 "os" 12 "path/filepath" 13 ) 14 15 func ExampleSplitList() { 16 fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin")) 17 // Output: 18 // On Unix: [/a/b/c /usr/bin] 19 } 20 21 func ExampleRel() { 22 paths := []string{ 23 "/a/b/c", 24 "/b/c", 25 "./b/c", 26 } 27 base := "/a" 28 29 fmt.Println("On Unix:") 30 for _, p := range paths { 31 rel, err := filepath.Rel(base, p) 32 fmt.Printf("%q: %q %v\n", p, rel, err) 33 } 34 35 // Output: 36 // On Unix: 37 // "/a/b/c": "b/c" <nil> 38 // "/b/c": "../b/c" <nil> 39 // "./b/c": "" Rel: can't make ./b/c relative to /a 40 } 41 42 func ExampleSplit() { 43 paths := []string{ 44 "/home/arnie/amelia.jpg", 45 "/mnt/photos/", 46 "rabbit.jpg", 47 "/usr/local//go", 48 } 49 fmt.Println("On Unix:") 50 for _, p := range paths { 51 dir, file := filepath.Split(p) 52 fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file) 53 } 54 // Output: 55 // On Unix: 56 // input: "/home/arnie/amelia.jpg" 57 // dir: "/home/arnie/" 58 // file: "amelia.jpg" 59 // input: "/mnt/photos/" 60 // dir: "/mnt/photos/" 61 // file: "" 62 // input: "rabbit.jpg" 63 // dir: "" 64 // file: "rabbit.jpg" 65 // input: "/usr/local//go" 66 // dir: "/usr/local//" 67 // file: "go" 68 } 69 70 func ExampleJoin() { 71 fmt.Println("On Unix:") 72 fmt.Println(filepath.Join("a", "b", "c")) 73 fmt.Println(filepath.Join("a", "b/c")) 74 fmt.Println(filepath.Join("a/b", "c")) 75 fmt.Println(filepath.Join("a/b", "/c")) 76 // Output: 77 // On Unix: 78 // a/b/c 79 // a/b/c 80 // a/b/c 81 // a/b/c 82 } 83 func ExampleWalk() { 84 dir := "dir/to/walk" 85 subDirToSkip := "skip" // dir/to/walk/skip 86 87 err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 88 if err != nil { 89 fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err) 90 return err 91 } 92 if info.IsDir() && info.Name() == subDirToSkip { 93 fmt.Printf("skipping a dir without errors: %+v \n", info.Name()) 94 return filepath.SkipDir 95 } 96 fmt.Printf("visited file: %q\n", path) 97 return nil 98 }) 99 100 if err != nil { 101 fmt.Printf("error walking the path %q: %v\n", dir, err) 102 } 103 }