github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/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 //go:build !windows && !plan9 6 // +build !windows,!plan9 7 8 package filepath_test 9 10 import ( 11 "fmt" 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 77 fmt.Println(filepath.Join("a/b", "../../../xyz")) 78 79 // Output: 80 // On Unix: 81 // a/b/c 82 // a/b/c 83 // a/b/c 84 // a/b/c 85 // ../xyz 86 } 87 88 func ExampleMatch() { 89 fmt.Println("On Unix:") 90 fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo")) 91 fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar")) 92 fmt.Println(filepath.Match("/home/?opher", "/home/gopher")) 93 fmt.Println(filepath.Match("/home/\\*", "/home/*")) 94 95 // Output: 96 // On Unix: 97 // true <nil> 98 // false <nil> 99 // true <nil> 100 // true <nil> 101 } 102 103 func ExampleBase() { 104 fmt.Println("On Unix:") 105 fmt.Println(filepath.Base("/foo/bar/baz.js")) 106 fmt.Println(filepath.Base("/foo/bar/baz")) 107 fmt.Println(filepath.Base("/foo/bar/baz/")) 108 fmt.Println(filepath.Base("dev.txt")) 109 fmt.Println(filepath.Base("../todo.txt")) 110 fmt.Println(filepath.Base("..")) 111 fmt.Println(filepath.Base(".")) 112 fmt.Println(filepath.Base("/")) 113 fmt.Println(filepath.Base("")) 114 115 // Output: 116 // On Unix: 117 // baz.js 118 // baz 119 // baz 120 // dev.txt 121 // todo.txt 122 // .. 123 // . 124 // / 125 // . 126 } 127 128 func ExampleDir() { 129 fmt.Println("On Unix:") 130 fmt.Println(filepath.Dir("/foo/bar/baz.js")) 131 fmt.Println(filepath.Dir("/foo/bar/baz")) 132 fmt.Println(filepath.Dir("/foo/bar/baz/")) 133 fmt.Println(filepath.Dir("/dirty//path///")) 134 fmt.Println(filepath.Dir("dev.txt")) 135 fmt.Println(filepath.Dir("../todo.txt")) 136 fmt.Println(filepath.Dir("..")) 137 fmt.Println(filepath.Dir(".")) 138 fmt.Println(filepath.Dir("/")) 139 fmt.Println(filepath.Dir("")) 140 141 // Output: 142 // On Unix: 143 // /foo/bar 144 // /foo/bar 145 // /foo/bar/baz 146 // /dirty/path 147 // . 148 // .. 149 // . 150 // . 151 // / 152 // . 153 } 154 155 func ExampleIsAbs() { 156 fmt.Println("On Unix:") 157 fmt.Println(filepath.IsAbs("/home/gopher")) 158 fmt.Println(filepath.IsAbs(".bashrc")) 159 fmt.Println(filepath.IsAbs("..")) 160 fmt.Println(filepath.IsAbs(".")) 161 fmt.Println(filepath.IsAbs("/")) 162 fmt.Println(filepath.IsAbs("")) 163 164 // Output: 165 // On Unix: 166 // true 167 // false 168 // false 169 // false 170 // true 171 // false 172 }