GO語言"path"包中"Clean"函數的用法及代碼示例。
用法:
func Clean(path string) string
Clean 通過純詞法處理返回與 path 等效的最短路徑名。它迭代地應用以下規則,直到無法進行進一步處理:
1. Replace multiple slashes with a single slash. 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.
僅當返回的路徑是根 "/" 時,返回的路徑才以斜線結尾。
如果此過程的結果是空字符串,則 Clean 返回字符串 "."。
另請參見 Rob Pike,“計劃 9 中的詞法文件名或獲取 Dot-Dot 正確”,https://9p.io/sys/doc/lexnames.html
例子:
package main
import (
"fmt"
"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))
}
}
輸出:
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("") = "."
相關用法
- GO Chmod用法及代碼示例
- GO Cmd.Start用法及代碼示例
- GO CanBackquote用法及代碼示例
- GO CopyN用法及代碼示例
- GO CopyBuffer用法及代碼示例
- GO CreateTemp用法及代碼示例
- GO Cut用法及代碼示例
- GO CIDRMask用法及代碼示例
- GO ContainsRune用法及代碼示例
- GO Cbrt用法及代碼示例
- GO Copysign用法及代碼示例
- GO Compare用法及代碼示例
- GO Cmd.Output用法及代碼示例
- GO Cosh用法及代碼示例
- GO Config用法及代碼示例
- GO Cmd.StdinPipe用法及代碼示例
- GO Cmd.StderrPipe用法及代碼示例
- GO Count用法及代碼示例
- GO Chtimes用法及代碼示例
- GO ContainsAny用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Clean。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。