<p>I have a use-case where I may need to work with linux filepaths on windows, or vice-versa.</p>
<p>It looks a lot like the assumption that you only need to work with paths that match your operating system, while certainly mostly correct, means that my best way of doing this looks to be to duplicate the filepath package. It feels like there ought to be a better solution than that, like how in http and some other packages you can use the module level default functions or create an instance of a client with the settings you need.</p>
<p>Does anyone know a better way of dealing with this issue?</p>
<hr/>**评论:**<br/><br/>nsd433: <pre><p>There is also the path package, which only handles / paths. That solves the unix filepaths on windows case, but not the other.</p></pre>Spirit_of_Stallman: <pre><p>Why you need to duplicate the filepath package? What problem with using <code>filepath.ToSlash</code> and <code>filepath.FromSlash</code> as imput/output wrapers?</p></pre>Redundancy_: <pre><p>Is that enough? I was worrying about any other edge cases that might be non-obvious.</p></pre>Spirit_of_Stallman: <pre><p>Solve problems as they come along.</p>
<p>If you need to work with something special or unique - no need to wait for a silver bullet or a miracle. Special requirements require special code. For common cases, "filepath" does its job well.</p></pre>neoasterisk: <pre><blockquote>
<p>I have a use-case where I may need to work with linux filepaths on windows, or vice-versa.</p>
</blockquote>
<p>I've had a similar use case like that. Or at least I thought I did.</p>
<p>First I did manual replace of slash. It was messy and there's lots of bugs. </p>
<p>Then I discovered <code>filepath.FromSlash</code> and <code>filepath.ToSlash</code>. Things improved but the system just seemed fragile and error prone to me.</p>
<p>So I reviewed the requirements and now I am planning to rebuild the software so that there will be an additional server component which will be doing the final conversion. This component will always be running on a Linux machine so things are going to be much clearer I believe.</p></pre>Redundancy_: <pre><p>Unfortunately the system that I connect to deals with both OSes interchangably, and passes raw paths on. I have to be able to deal with either case, and I can't do so at the source.</p></pre>neoasterisk: <pre><p>Well there has to be a way.</p>
<p>For example you could treat the Linux the system as the source of truth and only store that kind in your code and only do conversions for the windows machines.</p>
<p>Or you could try and do an abstraction of the path and only evaluate it lazily when you know the actual machine.</p>
<p>I don't know exactly what your system has to do so it's hard to propose a solution but there has to be some way.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传