I want to open a given file "directory/subdirectory/file.txt"
in golang. What is the recommended way to express such a path in an OS agnostic way (ie backslashes in Windows, forward slashes in Mac and Linux)? Something like Python's os.path
module?
如何在Go中创建跨平台文件路径?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- dsz90288 2012-02-21 06:47关注
For creating and manipulating OS-specific paths directly use
os.PathSeparator
and thepath/filepath
package.An alternative method is to always use
'/'
and thepath
package throughout your program. Thepath
package uses'/'
as path separator irrespective of the OS. Before opening or creating a file, convert the /-separated path into an OS-specific path string by callingfilepath.FromSlash(path string)
. Paths returned by the OS can be converted to /-separated paths by callingfilepath.ToSlash(path string)
.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报