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)
.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Qt下使用tcp获取数据的详细操作
- ¥15 idea右下角设置编码是灰色的
- ¥15 全志H618ROM新增分区
- ¥20 jupyter保存图像功能的实现
- ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
- ¥15 NAO机器人的录音程序保存问题
- ¥15 C#读写EXCEL文件,不同编译
- ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
- ¥15 扩散模型sd.webui使用时报错“Nonetype”
- ¥15 stm32流水灯+呼吸灯+外部中断按键