GO語言"path/filepath"包中"Rel"函數的用法及代碼示例。
用法:
func Rel(basepath, targpath string)(string, error)
Rel 返回一個在詞法上等效於 targpath 的相對路徑,當它通過中間分隔符連接到 basepath 時。也就是說,Join(basepath, Rel(basepath, targpath)) 等價於 targpath 本身。成功時,返回的路徑將始終相對於 basepath,即使 basepath 和 targpath 不共享任何元素。如果 targpath 不能相對於 basepath 或者如果需要知道當前工作目錄來計算它,則會返回錯誤。 Rel 根據結果調用 Clean。
例子:
package main
import (
"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)
}
}
輸出:
On Unix:
"/a/b/c": "b/c" <nil>
"/b/c": "../b/c" <nil>
"./b/c": "" Rel: can't make ./b/c relative to /a
相關用法
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Reverse用法及代碼示例
- GO Read用法及代碼示例
- GO Regexp.SubexpIndex用法及代碼示例
- GO Regexp.Match用法及代碼示例
- GO Remainder用法及代碼示例
- GO Regexp.Longest用法及代碼示例
- GO Replace用法及代碼示例
- GO ReadFile用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Rel。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。