Go語言程式獲取兩個絕對路徑的相對路徑
要獲取 Go 語言中兩個絕對路徑的相對路徑,我們使用 filepath 和 string 包。相對路徑相對於當前工作目錄指示檔案的位置,而絕對路徑則從根目錄開始指示檔案的位置。在第一種方法中,我們將使用 filepath 包函式,在第二種方法中,我們將使用 strings 包函式。
方法 1:使用 Filepath 包
在此程式中,使用 path/filepath 包中的 filepath.Base 函式提取檔案的基名,即不帶目錄路徑的檔名。然後,使用 fmt 包在控制檯上顯示提取的檔名。
語法
filepath.Rel
此函式用於查詢兩個檔案路徑之間的相對路徑。它接受兩個輸入 - 基路徑和目標路徑。
演算法
步驟 1 − 建立一個包 main 並宣告 fmt(格式化包)、path/filepath 包在程式中,其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。
步驟 2 − 建立一個函式 main,並在該函式中建立兩個路徑變數 path1 和 path 併為其分配絕對路徑。
步驟 3 − 現在使用 filepath.Rel 函式從兩個絕對路徑獲取相對路徑。
步驟 4 − 如果成功獲取路徑,則將其列印到控制檯,但如果未成功獲取,則在控制檯上列印錯誤並返回。
步驟 5 − 使用 fmt.Println() 函式執行列印語句
示例
在此示例中,我們將使用 filepath.Rel 函式從兩個絕對路徑獲取相對路徑。讓我們看看程式碼。
package main
import (
"fmt"
"path/filepath"
)
func main() {
Path1 := "/home/asus/user/src/github.com/folder1" //absolute path1
Path2 := "/home/asus/user/src/github.com/folder2" //absolute path2
relPath, err := filepath.Rel(Path1, Path2) //use the Rel function to get the relative path
if err != nil {
fmt.Println("Error:", err) //print error if no path is obtained
return
}
fmt.Println("Relative path:", relPath) //print the relative path
}
輸出
Relative path: ../folder2
方法 2:使用 Strings 包
在此方法中,首先使用 strings.Split 函式將絕對路徑劃分為其組成部分。在下一步中,迭代元件並進行比較以查詢兩個路徑之間的公共字首。如果路徑之間沒有公共字首,則返回錯誤。否則,將 Path1 中的剩餘元件首先新增,然後新增 Path2 中的剩餘元件,以建立相對路徑。
語法
func Split(str, sep string) []string
Split() 函式用於透過提供的分隔符分割字串。此函式存在於 strings 包中,它接受要分割的字串作為引數以及一個分隔符。然後,該函式返回最終的字串陣列作為結果。
func len(v Type) int
len() 函式用於獲取任何引數的長度。它將一個引數作為我們希望查詢其長度的資料型別變數,並返回一個整數,該整數是變數的長度。
演算法
步驟 1 − 建立一個包 main 並宣告 fmt(格式化包)、strings 包在程式中,其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。
步驟 2 − 建立一個函式 relativePath,其中定義了 main 函式中的兩個絕對路徑和一個字串型別的錯誤。
步驟 3 − 在此步驟中,使用 strings.Split 函式將路徑拆分為元件。
步驟 4 − 然後,迭代 components1 和 components2 以檢查它們是否具有任何公共字首或它們是否相等。
步驟 5 − 如果路徑沒有公共字首,則在控制檯上列印它。
步驟 6 − 然後,透過在絕對路徑的剩餘元件中新增 ../ 來構建相對路徑。
步驟 7 − 執行完所有先前的步驟後,將相對路徑返回到函式。
步驟 8 − 在 main 中,如果成功獲取相對路徑,它將列印到控制檯,但如果在獲取路徑時發生錯誤,則列印錯誤並返回。
步驟 9 − 使用 fmt.Println() 函式執行列印語句
示例
在此示例中,我們將使用 strings.Split 函式將路徑拆分為元件並構建相對路徑。
package main
import (
"fmt"
"strings"
)
func relativePath(Path1, Path2 string) (string, error) {
components1 := strings.Split(Path1, "/") //break the path into components
components2 := strings.Split(Path2, "/")
var i int
for i = 0; i < len(components1) && i < len(components2); i++ {
if components1[i] != components2[i] { //check if they are equal
break
}
}
if i == 0 {
return "", fmt.Errorf("Paths do not have a common prefix") //print error if thry don’t have a common prefix
}
var relativePath string
for j := i; j < len(components1)-1; j++ {
relativePath += "../" //append the directory into remaining components
}
for j := i; j < len(components2); j++ {
relativePath += components2[j] + "/"
}
return relativePath, nil //return the relativepath
}
func main() {
Path1 := "/home/user/go/src/github.com/project/folder1" //absolute path1
Path2 := "/home/user/go/src/github.com/project/folder2" //absolute path2
relPath, err := relativePath(Path1, Path2)
if err != nil {
fmt.Println("Error:", err) //if no path is obtained print error
return
}
fmt.Println("Relative path:", relPath) //print the relativepath
}
輸出
Relative path: folder2/
結論
我們使用兩個示例執行了從兩個絕對路徑獲取相對路徑的程式。在第一個示例中,我們使用 filepath.Rel 查詢相對路徑,在第二個示例中,我們使用 strings 包函式。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP