こんにちは、kurumi-bioです。
第4回目のfilepathパッケージ(標準ライブラリー)の学習です。
前回の記事
APIリファレンス(過去記事の一覧)
環境
- Windows
OSバージョン:Windows11 Home 22H2
Go言語のバージョン:go version go1.20.3 windows/amd64 - Linux
OSバージョン:openSUSE Leap 15.4
Go言語のバージョン:go version go1.20.3 linux/amd64
コードの説明
ファイルパス文字列の操作を行う関数ですので、色々な形式のパスを渡して結果を確認してみました。
Rel関数
func Rel(basepath, targpath string) (string, error)
package main import ( "fmt" "path/filepath" ) func printResult(b, t string) { s, e := filepath.Rel(b, t) if e != nil { fmt.Printf("Rel Error [%v] : basepath=[%s]/ targpath=[%s]\n", e, b, t) } else { fmt.Printf("\tbasepath=[%-20s] targpath=[%-30s] -> [%-20s]\n", b, t, s) } } func main() { println("◆ドライブ名で始まる(Windows形式)") //BasepathがTargetPathに含まれる場合 printResult("c:\\folder1", "c:\\folder1\\file1") //BasepathがTargetPathに含まれない場合 printResult("c:\\folder1", "c:\\folder2\\file2") //BasepathがTargetPathに含まれない場合 printResult("c:\\folder1\\folder2", "c:\\folder3\\folder4\\file5") println("◆パス区切り文字で始まる(Linux形式)") //BasepathがTargetPathに含まれる場合 printResult("/folder1", "/folder1/file1") //BasepathがBasepathに含まれない場合 printResult("/folder1", "/folder2/file2") //BasepathがTargetPathに含まれない場合 printResult("/folder1/folder2", "/folder3/folder4/file5") println("◆パス区切り文字で始まらない") printResult("folder1", "/folder1/file1") printResult("/folder1", "folder1/file1") printResult("folder1", "folder1/file1") printResult("folder1", "folder2/file1") }
◆実行結果(Windows)
◆実行結果(Linux)
Split関数
func Split(path string) (dir, file string)
package main import ( "fmt" "path/filepath" ) func printResult(p string) { d, f := filepath.Split(p) fmt.Printf("\tpath=[%-20s] -> dir=[%-20s] file=[%-10s]\n", p, d, f) } func main() { println("◆ドライブ名で始まる(Windows形式)") printResult("c:/file") printResult("c:/folder/file") println("◆パス区切り文字で始まる(Linux形式)") printResult("/folder") printResult("/folder/file") println("◆パス区切り文字で始まらない") printResult("folder1/folder2/file") printResult("file") }
◆実行結果(Windows)
◆実行結果(Linux)
SplitList関数
func SplitList(path string) []string
package main import ( "fmt" "path/filepath" ) func printResult(p string) { fmt.Printf("\t%s\n", p) l := filepath.SplitList(p) for i, v := range l { fmt.Printf("\t\tvalue(%d)=[%s]\n", i, v) } } func main() { println("◆ドライブ名で始まる(Windows形式)") printResult("c:\\file") printResult("c:\\folder\\file;d:\\folder2\\file2") println("◆パス区切り文字で始まる(Linux形式)") printResult("/folder") printResult("/folder/file;/folder2/file2") printResult("/folder/file:/folder2/file2") }
◆実行結果(Windows)
◆実行結果(Linux)
ToSlash関数
func ToSlash(path string) string
package main import ( "fmt" "path/filepath" ) func printResult(p string) { s := filepath.ToSlash(p) fmt.Printf("\tpath=[%-20s] -> [%-20s]\n", p, s) } func main() { println("◆ドライブ名で始まる(Windows形式)") printResult("c:\\file") printResult("c:\\folder\\file") println("◆パス区切り文字で始まる(Linux形式)") printResult("/folder") printResult("/folder/file") println("◆パス区切り文字で始まらない") printResult("folder1/folder2/file") printResult("file") }
◆実行結果(Windows)
◆実行結果(Linux)
最後に
今回も単純な文字列操作でした。
わざわざ関数が用意されていなくても、stringsパッケージの関数を使えば実装できそうな感じです。
Rel関数が、多少難易度が高そうな感じですし使い道もあるかなぁという感じですが、他の関数は使うことが無さそうです。
最後までご覧いただきありがとうございます