Source file src/path/example_test.go

     1  // Copyright 2012 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  package path_test
     6  
     7  import (
     8  	"fmt"
     9  	"path"
    10  )
    11  
    12  func ExampleBase() {
    13  	fmt.Println(path.Base("/a/b"))
    14  	fmt.Println(path.Base("/"))
    15  	fmt.Println(path.Base(""))
    16  	// Output:
    17  	// b
    18  	// /
    19  	// .
    20  }
    21  
    22  func ExampleClean() {
    23  	paths := []string{
    24  		"a/c",
    25  		"a//c",
    26  		"a/c/.",
    27  		"a/c/b/..",
    28  		"../a/c",
    29  		"../a/b/../././/c",
    30  		"/../a/c",
    31  		"/../a/b/../././/c",
    32  		"",
    33  	}
    34  
    35  	for _, p := range paths {
    36  		fmt.Printf("Clean(%q) = %q\n", p, path.Clean(p))
    37  	}
    38  
    39  	// Output:
    40  	// Clean("a/c") = "a/c"
    41  	// Clean("a//c") = "a/c"
    42  	// Clean("a/c/.") = "a/c"
    43  	// Clean("a/c/b/..") = "a/c"
    44  	// Clean("../a/c") = "../a/c"
    45  	// Clean("../a/b/../././/c") = "../a/c"
    46  	// Clean("/../a/c") = "/a/c"
    47  	// Clean("/../a/b/../././/c") = "/a/c"
    48  	// Clean("") = "."
    49  }
    50  
    51  func ExampleDir() {
    52  	fmt.Println(path.Dir("/a/b/c"))
    53  	fmt.Println(path.Dir("a/b/c"))
    54  	fmt.Println(path.Dir("/a/"))
    55  	fmt.Println(path.Dir("a/"))
    56  	fmt.Println(path.Dir("/"))
    57  	fmt.Println(path.Dir(""))
    58  	// Output:
    59  	// /a/b
    60  	// a/b
    61  	// /a
    62  	// a
    63  	// /
    64  	// .
    65  }
    66  
    67  func ExampleExt() {
    68  	fmt.Println(path.Ext("/a/b/c/bar.css"))
    69  	fmt.Println(path.Ext("/"))
    70  	fmt.Println(path.Ext(""))
    71  	// Output:
    72  	// .css
    73  	//
    74  	//
    75  }
    76  
    77  func ExampleIsAbs() {
    78  	fmt.Println(path.IsAbs("/dev/null"))
    79  	// Output: true
    80  }
    81  
    82  func ExampleJoin() {
    83  	fmt.Println(path.Join("a", "b", "c"))
    84  	fmt.Println(path.Join("a", "b/c"))
    85  	fmt.Println(path.Join("a/b", "c"))
    86  
    87  	fmt.Println(path.Join("a/b", "../../../xyz"))
    88  
    89  	fmt.Println(path.Join("", ""))
    90  	fmt.Println(path.Join("a", ""))
    91  	fmt.Println(path.Join("", "a"))
    92  
    93  	// Output:
    94  	// a/b/c
    95  	// a/b/c
    96  	// a/b/c
    97  	// ../xyz
    98  	//
    99  	// a
   100  	// a
   101  }
   102  
   103  func ExampleMatch() {
   104  	fmt.Println(path.Match("abc", "abc"))
   105  	fmt.Println(path.Match("a*", "abc"))
   106  	fmt.Println(path.Match("a*/b", "a/c/b"))
   107  	// Output:
   108  	// true <nil>
   109  	// true <nil>
   110  	// false <nil>
   111  }
   112  
   113  func ExampleSplit() {
   114  	split := func(s string) {
   115  		dir, file := path.Split(s)
   116  		fmt.Printf("path.Split(%q) = dir: %q, file: %q\n", s, dir, file)
   117  	}
   118  	split("static/myfile.css")
   119  	split("myfile.css")
   120  	split("")
   121  	// Output:
   122  	// path.Split("static/myfile.css") = dir: "static/", file: "myfile.css"
   123  	// path.Split("myfile.css") = dir: "", file: "myfile.css"
   124  	// path.Split("") = dir: "", file: ""
   125  }
   126  

View as plain text