dongqin8652 2019-06-28 08:28
浏览 73
已采纳

使用标准库将与基础操作系统无关的反斜杠分隔符连接路径

filepath.Join allows to join paths by using the path separator of the underlying os (basically \ on Windows, / everywhere else).

Is there a way to force using the Windows path separator \ for joining paths with the Go standard library no matter what the actual running operating system is?

  • 写回答

1条回答 默认 最新

  • douhuang2673 2019-06-28 08:35
    关注

    No, not with the standard lib's path/filepath package. You have to use strings.Join() instead.

    If you do rely on "side-effects" of filepath.Join() (e.g. adding a separator if necessary, calling Clean() etc.), then easiest would be to call filepath.Join(), then replace slashes with backslashes, e.g. using strings.ReplaceAll().

    Note that the replace-all method works because slash / is a reserved character and cannot appear in names other than separating folders and files. For details, see Naming Files, Paths, and Namespaces.

    See related filepath.Join removes dot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法