?
このドキュメントでは、 php中國語ネットマニュアル リリース
import "path/filepath"
概述
索引
例子
軟件包文件路徑以與目標操作系統(tǒng)定義的文件路徑兼容的方式實現(xiàn)用于操作文件名路徑的實用程序例程。
filepath 包使用正斜杠或反斜杠,具體取決于操作系統(tǒng)。要處理不管操作系統(tǒng)如何都始終使用正斜杠的 URL,請參閱路徑包。
常量
Variables
func Abs(path string) (string, error)
func Base(path string) string
func Clean(path string) string
func Dir(path string) string
func EvalSymlinks(path string) (string, error)
func Ext(path string) string
func FromSlash(path string) string
func Glob(pattern string) (matches []string, err error)
func HasPrefix(p, prefix string) bool
func IsAbs(path string) bool
func Join(elem ...string) string
func Match(pattern, name string) (matched bool, err error)
func Rel(basepath, targpath string) (string, error)
func Split(path string) (dir, file string)
func SplitList(path string) []string
func ToSlash(path string) string
func VolumeName(path string) string
func Walk(root string, walkFn WalkFunc) error
type WalkFunc
Join Rel Split SplitList
match.go path.go path_unix.go symlink.go symlink_unix.go
const ( Separator = os.PathSeparator ListSeparator = os.PathListSeparator)
ErrBadPattern 表示通配模式格式錯誤。
var ErrBadPattern = errors.New("syntax error in pattern")
SkipDir 被用作來自 WalkFuncs 的返回值,以指示呼叫中指定的目錄將被跳過。它不會被任何函數(shù)返回為錯誤。
var SkipDir = errors.New("skip this directory")
func Abs(path string) (string, error)
Abs 返回路徑的絕對表示。如果路徑不是絕對路徑,它將與當前工作目錄連接,將其變?yōu)榻^對路徑。并不保證給定文件的絕對路徑名是唯一的。Abs 在結果上調(diào)用 Clean 。
func Base(path string) string
Base 返回路徑的最后一個元素。在提取最后一個元素之前,刪除了尾部路徑分隔符。如果路徑為空,則 Base 返回“。”。如果路徑完全由分隔符組成,則 Base 將返回一個分隔符。
func Clean(path string) string
Clean 通過純詞法處理返回等價于路徑的最短路徑名。它反復應用以下規(guī)則,直到不能進行進一步的處理:
1. Replace multiple Separator elements with a single one.2. Eliminate each . path name element (the current directory).3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path, assuming Separator is '/'.
僅當返回的路徑表示根目錄(例如 Unix 或C:\
Windows 上的“/”)時,才以斜線結尾。
最后,任何出現(xiàn)的斜杠被分隔符替換。
如果此進程的結果是空字符串,則 Clean 將返回字符串“?!?。
func Dir(path string) string
Dir 返回路徑的最后一個元素,通常是路徑的目錄。刪除最后一個元素后,Dir 在路徑上調(diào)用 Clean 并刪除尾部斜線。如果路徑為空,則 Dir 返回“?!?。如果路徑完全由分隔符組成,則 Dir 返回一個分隔符。返回的路徑不會以分隔符結尾,除非它是根目錄。
func EvalSymlinks(path string) (string, error)
EvalSymlinks 在評估任何符號鏈接后返回路徑名稱。如果路徑是相對的,則結果將與當前目錄相關,除非其中一個組件是絕對符號鏈接。EvalSymlinks 在結果上調(diào)用 Clean 。
func Ext(path string) string
Ext 返回 path 使用的文件擴展名。擴展名是從路徑最后一個元素的最后一個點開始的后綴; 如果沒有點,它是空的。
func FromSlash(path string) string
FromSlash 返回用分隔符替換路徑中每個斜杠('/')字符的結果。多個斜杠被多個分隔符替代。
func Glob(pattern string) (matches []string, err error)
如果沒有匹配的文件,Glob 返回匹配模式的所有文件的名稱或nil。模式的語法與 Match 中的相同。該模式可以描述分層名稱,例如 / usr / * / bin / ed(假設分隔符是'/')。
Glob 忽略文件系統(tǒng)錯誤,例如讀取目錄的 I/O 錯誤。當模式格式錯誤時,唯一可能返回的錯誤是 ErrBadPattern 。
func HasPrefix(p, prefix string) bool
HasPrefix 存在歷史兼容性,不應使用。
棄用:HasPrefix 不遵守路徑邊界,并且在需要時不會忽略大小寫。
func IsAbs(path string) bool
IsAbs 報告路徑是否絕對。
func Join(elem ...string) string
Join 將任意數(shù)量的路徑元素連接到單個路徑中,如有必要添加分隔符。加入調(diào)用清理結果; 特別是,所有空串都被忽略。在 Windows 上,當且僅當?shù)谝粋€路徑元素是 UNC 路徑時,結果為 UNC 路徑。
package mainimport ("fmt""path/filepath")func main() { fmt.Println("On Unix:") fmt.Println(filepath.Join("a", "b", "c")) fmt.Println(filepath.Join("a", "b/c")) fmt.Println(filepath.Join("a/b", "c")) fmt.Println(filepath.Join("a/b", "/c"))}
func Match(pattern, name string) (matched bool, err error)
匹配報告名稱是否與 shell 文件名稱模式相匹配。模式語法是:
pattern:{ term }term:'*' matches any sequence of non-Separator characters'?' matches any single non-Separator 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
匹配需要匹配所有名稱的模式,而不僅僅是一個子字符串。當模式格式錯誤時,唯一可能返回的錯誤是 ErrBadPattern 。
在 Windows 上,轉義被禁用。相反,'\'被視為路徑分隔符。
func Rel(basepath, targpath string) (string, error)
當通過介入分隔符連接到 basepath 時,Rel 返回一個與 targpath 詞法相同的相對路徑。也就是說,Join(basepath,Rel(basepath,targpath))等同于 targpath 本身。成功時,即使 basepath 和 targpath 不共享任何元素,返回的路徑也總是相對于 basepath 。如果無法相對于基本路徑創(chuàng)建 targpath,或者如果知道當前工作目錄來計算它,則會返回錯誤。Rel 調(diào)用清理結果。
package mainimport ("fmt""path/filepath")func main() { paths := []string{"/a/b/c","/b/c","./b/c",} base := "/a" fmt.Println("On Unix:")for _, p := range paths { rel, err := filepath.Rel(base, p) fmt.Printf("%q: %q %v\n", p, rel, err)}}
func Split(path string) (dir, file string)
Split 在最后一個 Separator 之后立即拆分路徑,將其分隔成一個目錄和文件名組件。如果路徑中沒有分隔符,則 Split 將返回一個空的目錄并將文件設置為路徑。返回的值具有 path = dir + file 的屬性。
package mainimport ("fmt""path/filepath")func main() { paths := []string{"/home/arnie/amelia.jpg","/mnt/photos/","rabbit.jpg","/usr/local//go",} fmt.Println("On Unix:")for _, p := range paths { dir, file := filepath.Split(p) fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file)}}
func SplitList(path string) []string
SplitList 拆分由特定于操作系統(tǒng)的 ListSeparator 連接的路徑列表,通常在 PATH 或 GOPATH 環(huán)境變量中找到。與 strings.Split 不同,SplitList 在傳遞空字符串時返回空片段。
package mainimport ("fmt""path/filepath")func main() { fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin"))}
func ToSlash(path string) string
ToSlash 返回用斜線('/')字符替換路徑中每個分隔符的結果。多個分隔符被多個斜線替代。
func VolumeName(path string) string
VolumeName 返回領先的卷名稱。給定“C:\ foo \ bar”,它會在 Windows 上返回“C:”。鑒于“\host\share\foo”它返回“\host\share”。在其他平臺上,它返回“”。
func Walk(root string, walkFn WalkFunc) error
Walk 遍歷以根為根的文件樹,為樹中的每個文件或目錄(包括根)調(diào)用 walkFn 。所有訪問文件和目錄的錯誤都由 walkFn 過濾。這些文件按照詞匯順序走,這使得輸出具有確定性,但意味著對于非常大的目錄,Walk 可能效率低下。走并不遵循符號鏈接。
WalkFunc 是 Walk 所訪問的每個文件或目錄所調(diào)用的函數(shù)的類型。path 參數(shù)包含 Walk 作為前綴的參數(shù); 也就是說,如果使用包含文件“a”的目錄“dir”調(diào)用 Walk,則會使用參數(shù)“dir / a”調(diào)用 walk 功能。info 參數(shù)是指定路徑的 os.FileInfo 。
如果出現(xiàn)問題時走到由路徑命名的文件或目錄,傳入的錯誤將描述問題,并且函數(shù)可以決定如何處理該錯誤(并且 Walk 不會進入該目錄)。如果返回錯誤,則處理停止。唯一的例外是函數(shù)返回特殊值 SkipDir 。如果函數(shù)在目錄上調(diào)用時返回 SkipDir,則 Walk 完全跳過目錄的內(nèi)容。如果函數(shù)在非目錄文件上調(diào)用時返回 SkipDir,則 Walk 會跳過包含目錄中的其余文件。
type WalkFunc func(path string, info os.FileInfo, err error) error