diff options
Diffstat (limited to 'devdocs/go/path%2Findex.html')
| -rw-r--r-- | devdocs/go/path%2Findex.html | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/devdocs/go/path%2Findex.html b/devdocs/go/path%2Findex.html new file mode 100644 index 00000000..1a65498c --- /dev/null +++ b/devdocs/go/path%2Findex.html @@ -0,0 +1,132 @@ +<h1> Package path </h1> <ul id="short-nav"> +<li><code>import "path"</code></li> +<li><a href="#pkg-overview" class="overviewLink">Overview</a></li> +<li><a href="#pkg-index" class="indexLink">Index</a></li> +<li><a href="#pkg-examples" class="examplesLink">Examples</a></li> +<li><a href="#pkg-subdirectories">Subdirectories</a></li> +</ul> <h2 id="pkg-overview">Overview </h2> <p>Package path implements utility routines for manipulating slash-separated paths. </p> +<p>The path package should only be used for paths separated by forward slashes, such as the paths in URLs. This package does not deal with Windows paths with drive letters or backslashes; to manipulate operating system paths, use the <span>path/filepath</span> package. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav"> +<li><a href="#pkg-variables">Variables</a></li> +<li><a href="#Base">func Base(path string) string</a></li> +<li><a href="#Clean">func Clean(path string) string</a></li> +<li><a href="#Dir">func Dir(path string) string</a></li> +<li><a href="#Ext">func Ext(path string) string</a></li> +<li><a href="#IsAbs">func IsAbs(path string) bool</a></li> +<li><a href="#Join">func Join(elem ...string) string</a></li> +<li><a href="#Match">func Match(pattern, name string) (matched bool, err error)</a></li> +<li><a href="#Split">func Split(path string) (dir, file string)</a></li> +</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Base">Base</a></dd> <dd><a class="exampleLink" href="#example_Clean">Clean</a></dd> <dd><a class="exampleLink" href="#example_Dir">Dir</a></dd> <dd><a class="exampleLink" href="#example_Ext">Ext</a></dd> <dd><a class="exampleLink" href="#example_IsAbs">IsAbs</a></dd> <dd><a class="exampleLink" href="#example_Join">Join</a></dd> <dd><a class="exampleLink" href="#example_Match">Match</a></dd> <dd><a class="exampleLink" href="#example_Split">Split</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>match.go</span> <span>path.go</span> </p> <h2 id="pkg-variables">Variables</h2> <p>ErrBadPattern indicates a pattern was malformed. </p> +<pre data-language="go">var ErrBadPattern = errors.New("syntax error in pattern")</pre> <h2 id="Base">func <span>Base</span> </h2> <pre data-language="go">func Base(path string) string</pre> <p>Base returns the last element of path. Trailing slashes are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of slashes, Base returns "/". </p> <h4 id="example_Base"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.Base("/a/b")) +fmt.Println(path.Base("/")) +fmt.Println(path.Base("")) +</pre> <p>Output:</p> <pre class="output" data-language="go">b +/ +. +</pre> <h2 id="Clean">func <span>Clean</span> </h2> <pre data-language="go">func Clean(path string) string</pre> <p>Clean returns the shortest path name equivalent to path by purely lexical processing. It applies the following rules iteratively until no further processing can be done: </p> +<ol> <li>Replace multiple slashes with a single slash. </li> +<li>Eliminate each . path name element (the current directory). </li> +<li>Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it. </li> +<li>Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path. </li> +</ol> <p>The returned path ends in a slash only if it is the root "/". </p> +<p>If the result of this process is an empty string, Clean returns the string ".". </p> +<p>See also Rob Pike, “Lexical File Names in Plan 9 or Getting Dot-Dot Right,” <a href="https://9p.io/sys/doc/lexnames.html">https://9p.io/sys/doc/lexnames.html</a> </p> <h4 id="example_Clean"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">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)) +} + +</pre> <p>Output:</p> <pre class="output" data-language="go">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("") = "." +</pre> <h2 id="Dir">func <span>Dir</span> </h2> <pre data-language="go">func Dir(path string) string</pre> <p>Dir returns all but the last element of path, typically the path's directory. After dropping the final element using <a href="#Split">Split</a>, the path is Cleaned and trailing slashes are removed. If the path is empty, Dir returns ".". If the path consists entirely of slashes followed by non-slash bytes, Dir returns a single slash. In any other case, the returned path does not end in a slash. </p> <h4 id="example_Dir"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.Dir("/a/b/c")) +fmt.Println(path.Dir("a/b/c")) +fmt.Println(path.Dir("/a/")) +fmt.Println(path.Dir("a/")) +fmt.Println(path.Dir("/")) +fmt.Println(path.Dir("")) +</pre> <p>Output:</p> <pre class="output" data-language="go">/a/b +a/b +/a +a +/ +. +</pre> <h2 id="Ext">func <span>Ext</span> </h2> <pre data-language="go">func Ext(path string) string</pre> <p>Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot. </p> <h4 id="example_Ext"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.Ext("/a/b/c/bar.css")) +fmt.Println(path.Ext("/")) +fmt.Println(path.Ext("")) +</pre> <p>Output:</p> <pre class="output" data-language="go">.css +</pre> <h2 id="IsAbs">func <span>IsAbs</span> </h2> <pre data-language="go">func IsAbs(path string) bool</pre> <p>IsAbs reports whether the path is absolute. </p> <h4 id="example_IsAbs"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.IsAbs("/dev/null")) +</pre> <p>Output:</p> <pre class="output" data-language="go">true +</pre> <h2 id="Join">func <span>Join</span> </h2> <pre data-language="go">func Join(elem ...string) string</pre> <p>Join joins any number of path elements into a single path, separating them with slashes. Empty elements are ignored. The result is Cleaned. However, if the argument list is empty or all its elements are empty, Join returns an empty string. </p> <h4 id="example_Join"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.Join("a", "b", "c")) +fmt.Println(path.Join("a", "b/c")) +fmt.Println(path.Join("a/b", "c")) + +fmt.Println(path.Join("a/b", "../../../xyz")) + +fmt.Println(path.Join("", "")) +fmt.Println(path.Join("a", "")) +fmt.Println(path.Join("", "a")) + +</pre> <p>Output:</p> <pre class="output" data-language="go">a/b/c +a/b/c +a/b/c +../xyz + +a +a +</pre> <h2 id="Match">func <span>Match</span> </h2> <pre data-language="go">func Match(pattern, name string) (matched bool, err error)</pre> <p>Match reports whether name matches the shell pattern. The pattern syntax is: </p> +<pre data-language="go">pattern: + { term } +term: + '*' matches any sequence of non-/ characters + '?' matches any single non-/ character + '[' [ '^' ] { character-range } ']' + character class (must be non-empty) + c matches character c (c != '*', '?', '\\', '[') + '\\' c matches character c + +character-range: + c matches character c (c != '\\', '-', ']') + '\\' c matches character c + lo '-' hi matches character c for lo <= c <= hi +</pre> <p>Match requires pattern to match all of name, not just a substring. The only possible returned error is <a href="#ErrBadPattern">ErrBadPattern</a>, when pattern is malformed. </p> <h4 id="example_Match"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">fmt.Println(path.Match("abc", "abc")) +fmt.Println(path.Match("a*", "abc")) +fmt.Println(path.Match("a*/b", "a/c/b")) +</pre> <p>Output:</p> <pre class="output" data-language="go">true <nil> +true <nil> +false <nil> +</pre> <h2 id="Split">func <span>Split</span> </h2> <pre data-language="go">func Split(path string) (dir, file string)</pre> <p>Split splits path immediately following the final slash, separating it into a directory and file name component. If there is no slash in path, Split returns an empty dir and file set to path. The returned values have the property that path = dir+file. </p> <h4 id="example_Split"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go">split := func(s string) { + dir, file := path.Split(s) + fmt.Printf("path.Split(%q) = dir: %q, file: %q\n", s, dir, file) +} +split("static/myfile.css") +split("myfile.css") +split("") +</pre> <p>Output:</p> <pre class="output" data-language="go">path.Split("static/myfile.css") = dir: "static/", file: "myfile.css" +path.Split("myfile.css") = dir: "", file: "myfile.css" +path.Split("") = dir: "", file: "" +</pre> <h2 id="pkg-subdirectories">Subdirectories</h2> <div class="pkg-dir"> <table> <tr> <th class="pkg-name">Name</th> <th class="pkg-synopsis">Synopsis</th> </tr> <tr> <td colspan="2"><a href="../index">..</a></td> </tr> <tr> <td class="pkg-name"> <a href="filepath/index">filepath</a> </td> <td class="pkg-synopsis"> Package filepath implements utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths. </td> </tr> </table> </div><div class="_attribution"> + <p class="_attribution-p"> + © Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br> + <a href="http://golang.org/pkg/path/" class="_attribution-link">http://golang.org/pkg/path/</a> + </p> +</div> |
