path

package
v1.21.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

パッケージパスは、スラッシュで区切られたパスを操作するためのユーティリティルーチンを実装します。

pathパッケージは、URL内のパスなど、スラッシュで区切られたパスにのみ使用するべきです。このパッケージは、ドライブレターやバックスラッシュを含むWindowsパスを扱いません。オペレーティングシステムのパスを操作する場合は、path/filepathパッケージを使用してください。

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = errors.New("syntax error in pattern")

ErrBadPatternは、パターンが不正であることを示します。

Functions

func Base

func Base(path string) string

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

func Clean(path string) string

Clean関数は、パスを純粋に字句処理して、最短のパス名に変換します。 以下のルールを反復処理して、処理できる限り適用します:

  1. 連続するスラッシュを単一のスラッシュに置き換えます。
  2. 各「.」パス名要素(現在のディレクトリ)を削除します。
  3. 各「..」パス名要素(親ディレクトリ)とそれに先行する「..」以外の要素を削除します。
  4. ルートパスを始める「..」要素を削除します: つまり、パスの先頭にある「/..」を「/」に置き換えます。

返されるパスの末尾には、ルート「/」の場合にのみスラッシュがあります。

この処理結果が空の文字列の場合、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

func Dir(path string) string

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

func Ext(path string) string

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

func IsAbs(path string) bool

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

func Join(elem ...string) string

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

func Match(pattern, name string) (matched bool, err error)

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

func Split(path string) (dir, file string)

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.

Directories

Path Synopsis
Package filepathは、ファイル名パスを操作するためのユーティリティ関数を実装しています。
Package filepathは、ファイル名パスを操作するためのユーティリティ関数を実装しています。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL