Documentation
¶
Overview ¶
パッケージパスは、スラッシュで区切られたパスを操作するためのユーティリティルーチンを実装します。
pathパッケージは、URL内のパスなど、スラッシュで区切られたパスにのみ使用するべきです。このパッケージは、ドライブレターやバックスラッシュを含むWindowsパスを扱いません。オペレーティングシステムのパスを操作する場合は、path/filepathパッケージを使用してください。
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadPattern = errors.New("syntax error in pattern")
ErrBadPatternは、パターンが不正であることを示します。
Functions ¶
func Base ¶
Baseはパスの最後の要素を返します。 最後の要素を抽出する前に、トレーリングスラッシュは削除されます。 パスが空の場合、Baseは「.」を返します。 パスがすべてのスラッシュで構成されている場合、Baseは「/」を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { fmt.Println(path.Base("/a/b")) fmt.Println(path.Base("/")) fmt.Println(path.Base("")) }
Output: b / .
func Clean ¶
Clean関数は、パスを純粋に字句処理して、最短のパス名に変換します。 以下のルールを反復処理して、処理できる限り適用します:
- 連続するスラッシュを単一のスラッシュに置き換えます。
- 各「.」パス名要素(現在のディレクトリ)を削除します。
- 各「..」パス名要素(親ディレクトリ)とそれに先行する「..」以外の要素を削除します。
- ルートパスを始める「..」要素を削除します: つまり、パスの先頭にある「/..」を「/」に置き換えます。
返されるパスの末尾には、ルート「/」の場合にのみスラッシュがあります。
この処理結果が空の文字列の場合、Clean関数は「.」という文字列を返します。
参考文献:Rob Pike, “Lexical File Names in Plan 9 or Getting Dot-Dot Right,” https://9p.io/sys/doc/lexnames.html
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { 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)) } }
Output: 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("") = "."
func Dir ¶
Dirはパスの最後の要素以外のすべてを返します。通常はパスのディレクトリです。 Splitを使用して最後の要素を削除した後、パスはクリーン化され、末尾のスラッシュは削除されます。 パスが空の場合、Dirは "." を返します。 パスがスラッシュだけで構成され、スラッシュ以外のバイトが続く場合、Dirは単一のスラッシュを返します。それ以外の場合、返されるパスはスラッシュで終わりません。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { 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("")) }
Output: /a/b a/b /a a / .
func Ext ¶
Extは、pathで使用されるファイル名の拡張子を返します。 拡張子は、pathの最後のスラッシュで区切られた要素の最後のドットから始まるサフィックスです。 ドットが存在しない場合は空です。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { fmt.Println(path.Ext("/a/b/c/bar.css")) fmt.Println(path.Ext("/")) fmt.Println(path.Ext("")) }
Output: .css
func IsAbs ¶
IsAbsはパスが絶対パスかどうかを報告します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { fmt.Println(path.IsAbs("/dev/null")) }
Output: true
func Join ¶
Joinは任意の数のパス要素をスラッシュで区切って1つのパスに結合します。空の要素は無視されます。結果はクリーンになります。ただし、引数リストが空であるか、その要素がすべて空である場合、Joinは空の文字列を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { 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")) }
Output: a/b/c a/b/c a/b/c ../xyz a a
func Match ¶
Matchはnameがシェルパターンと一致するかどうかを判定します。 パターンの構文は以下のようになります:
パターン:
{ 要素 }
要素:
'*' 非/の任意のシーケンスに一致します '?' 非/の任意の1文字に一致します '[' [ '^' ] { 文字範囲 } ']' 文字のクラス(空でなければならない) c 文字cに一致します(c != '*', '?', '\\', '[') '\\' c 文字cに一致します
文字範囲:
c 文字cに一致します(c != '\\', '-', ']') '\\' c 文字cに一致します lo '-' hi lo <= c <= hiの範囲の文字cに一致します
Matchはパターンがname全体と一致する必要があります。部分一致ではありません。 返される可能性のある唯一のエラーは、patternが正しくない場合のErrBadPatternです。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { fmt.Println(path.Match("abc", "abc")) fmt.Println(path.Match("a*", "abc")) fmt.Println(path.Match("a*/b", "a/c/b")) }
Output: true <nil> true <nil> false <nil>
func Split ¶
Splitは最後のスラッシュの直後にパスを分割し、 ディレクトリとファイル名のコンポーネントに分ける。 パスにスラッシュがない場合、Splitは空のディレクトリと ファイルをpathに設定して返します。 返される値は、path = dir + fileという性質を持っています。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/path" ) func main() { 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("") }
Output: path.Split("static/myfile.css") = dir: "static/", file: "myfile.css" path.Split("myfile.css") = dir: "", file: "myfile.css" path.Split("") = dir: "", file: ""
Types ¶
This section is empty.