duangu4943 2016-08-27 13:41
浏览 640
已采纳

Go中path和path.filepath包之间有什么区别

I have found that there are lots of similar function in the package path and the package path/filepath. I have tried several common paths like /var/log/something but did not find any differences. When should one use path directly and when should one use filepath instead?

  • 写回答

1条回答 默认 最新

  • doubo82706 2016-08-27 14:17
    关注

    What is the difference?

    While functionally similar, path and path/filepath offer differing implementations. Filepath depends on the os package to choose the target runtime's file separators and other differing components when dealing with path strings.

    You can look as the os source to see that there are differing implementations for various utility functions. This allows operating system specific details to be abstracted away by the library and helps achieve portability. The path/filepath dependency graph illustrates how the package depends upon the os package. You can compare this with the path dependency graph. I would encourage you to go into the filepath and path source code to observe this relationship.

    When do I use each?

    You should use filepath when working with files. This ensures your paths will be matched with actual files regardless of the underlying runtime. The path library should be used within models or when paths may be serialized or communicated with other programs. This ensures that a single formatting scheme is used regardless of what platform the programming is running on. Having a conistent format makes reasoning about models more generic and easier to understand.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号