我想"directory/subdirectory/file.txt"在golang中打開一個給定的文件。建議使用哪種方式以操作系統(tǒng)不可知的方式(例如Windows中的反斜杠,Mac和Linux中的正斜杠)來表達這種路徑?像Python的os.path模塊一樣?
3 回答

慕虎7371278
TA貢獻1802條經(jīng)驗 獲得超4個贊
使用path/filepath
代替path
。path
僅用于正斜杠分隔的路徑(例如URL中使用的路徑),同時path/filepath
操縱跨不同操作系統(tǒng)的路徑。

陪伴而非守候
TA貢獻1757條經(jīng)驗 獲得超8個贊
創(chuàng)建了以下代碼:
package main
import (
? ? "fmt"
? ? "os"
? ? "path/filepath"
)
func main() {
? ? p := filepath.FromSlash("path/to/file")
? ? fmt.Println("Path: " + p)
}
返回:
Path: path\to\file
在Windows上。
- 3 回答
- 0 關注
- 317 瀏覽
添加回答
舉報
0/150
提交
取消