github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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 "path/filepath" 12 ) 13 14 func ExampleSplitList() { 15 fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin")) 16 // Output: 17 // On Unix: [/a/b/c /usr/bin] 18 } 19 20 func ExampleRel() { 21 paths := []string{ 22 "/a/b/c", 23 "/b/c", 24 "./b/c", 25 } 26 base := "/a" 27 28 fmt.Println("On Unix:") 29 for _, p := range paths { 30 rel, err := filepath.Rel(base, p) 31 fmt.Printf("%q: %q %v\n", p, rel, err) 32 } 33 34 // Output: 35 // On Unix: 36 // "/a/b/c": "b/c" <nil> 37 // "/b/c": "../b/c" <nil> 38 // "./b/c": "" Rel: can't make ./b/c relative to /a 39 } 40 41 func ExampleSplit() { 42 paths := []string{ 43 "/home/arnie/amelia.jpg", 44 "/mnt/photos/", 45 "rabbit.jpg", 46 "/usr/local//go", 47 } 48 fmt.Println("On Unix:") 49 for _, p := range paths { 50 dir, file := filepath.Split(p) 51 fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file) 52 } 53 // Output: 54 // On Unix: 55 // input: "/home/arnie/amelia.jpg" 56 // dir: "/home/arnie/" 57 // file: "amelia.jpg" 58 // input: "/mnt/photos/" 59 // dir: "/mnt/photos/" 60 // file: "" 61 // input: "rabbit.jpg" 62 // dir: "" 63 // file: "rabbit.jpg" 64 // input: "/usr/local//go" 65 // dir: "/usr/local//" 66 // file: "go" 67 } 68 69 func ExampleJoin() { 70 fmt.Println("On Unix:") 71 fmt.Println(filepath.Join("a", "b", "c")) 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 // Output: 76 // On Unix: 77 // a/b/c 78 // a/b/c 79 // a/b/c 80 // a/b/c 81 } 82 83 func ExampleMatch() { 84 fmt.Println("On Unix:") 85 fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo")) 86 fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar")) 87 fmt.Println(filepath.Match("/home/?opher", "/home/gopher")) 88 fmt.Println(filepath.Match("/home/\\*", "/home/*")) 89 90 // Output: 91 // On Unix: 92 // true <nil> 93 // false <nil> 94 // true <nil> 95 // true <nil> 96 } 97 98 func ExampleBase() { 99 fmt.Println("On Unix:") 100 fmt.Println(filepath.Base("/foo/bar/baz.js")) 101 fmt.Println(filepath.Base("/foo/bar/baz")) 102 fmt.Println(filepath.Base("/foo/bar/baz/")) 103 fmt.Println(filepath.Base("dev.txt")) 104 fmt.Println(filepath.Base("../todo.txt")) 105 fmt.Println(filepath.Base("..")) 106 fmt.Println(filepath.Base(".")) 107 fmt.Println(filepath.Base("/")) 108 fmt.Println(filepath.Base("")) 109 110 // Output: 111 // On Unix: 112 // baz.js 113 // baz 114 // baz 115 // dev.txt 116 // todo.txt 117 // .. 118 // . 119 // / 120 // . 121 } 122 123 func ExampleDir() { 124 fmt.Println("On Unix:") 125 fmt.Println(filepath.Dir("/foo/bar/baz.js")) 126 fmt.Println(filepath.Dir("/foo/bar/baz")) 127 fmt.Println(filepath.Dir("/foo/bar/baz/")) 128 fmt.Println(filepath.Dir("/dirty//path///")) 129 fmt.Println(filepath.Dir("dev.txt")) 130 fmt.Println(filepath.Dir("../todo.txt")) 131 fmt.Println(filepath.Dir("..")) 132 fmt.Println(filepath.Dir(".")) 133 fmt.Println(filepath.Dir("/")) 134 fmt.Println(filepath.Dir("")) 135 136 // Output: 137 // On Unix: 138 // /foo/bar 139 // /foo/bar 140 // /foo/bar/baz 141 // /dirty/path 142 // . 143 // .. 144 // . 145 // . 146 // / 147 // . 148 } 149 150 func ExampleIsAbs() { 151 fmt.Println("On Unix:") 152 fmt.Println(filepath.IsAbs("/home/gopher")) 153 fmt.Println(filepath.IsAbs(".bashrc")) 154 fmt.Println(filepath.IsAbs("..")) 155 fmt.Println(filepath.IsAbs(".")) 156 fmt.Println(filepath.IsAbs("/")) 157 fmt.Println(filepath.IsAbs("")) 158 159 // Output: 160 // On Unix: 161 // true 162 // false 163 // false 164 // false 165 // true 166 // false 167 }