<p>So i have a little problem with copy function,that i'll mention below.</p>
<p>When i want to copy file to another drive it creates <strong>new</strong> file with <strong>new</strong> date of creating and this is not so good - what is another way to do it?</p>
<p>And is there some kind of way to display progress of copying(i mean like 60% completed and so on)?</p>
<pre><code>func CopyNSFW(src, dst string) {
from, err := os.Open(src)
if err != nil {
log.Fatal(err)
}
defer from.Close()
to, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
log.Fatal(err)
}
</code></pre>
<p>} </p>
<hr/>**评论:**<br/><br/>maximch: <pre><p>Terminal progress bar for Go:
<a href="https://github.com/cheggaaa/pb" rel="nofollow">https://github.com/cheggaaa/pb</a></p></pre>DizzyVik: <pre><p>For progress - look at this: <a href="https://play.golang.org/p/N6xL8_fnV2" rel="nofollow">https://play.golang.org/p/N6xL8_fnV2</a></p>
<p>As for the time - I'm not sure about the creation time, but <a href="https://golang.org/pkg/os/#Chtimes" rel="nofollow">os.Chtimes</a> could help with the modification date...</p></pre>vattghern: <pre><p>So there is no way to move file simply right? I mean I must use copy to transfer file to another drive, and now to transform date to workaround it, right?</p></pre>titpetric: <pre><p>You might be looking for <a href="https://golang.org/pkg/os/#Rename" rel="nofollow">os.Rename</a>?</p></pre>vattghern: <pre><p>But it cannot rename on different drives</p></pre>titpetric: <pre><p>Oh yeah. sure. So you're correct, you should perform a copy and then update whatever m-times and such if you need to. I asumed that the stdlib would be equivalent to <code>mv file file2</code>, but it isn't as it's basically just the rename syscall.</p></pre>vattghern: <pre><p>And that is sad story...
Anyway tnx for reply</p></pre>dgryski: <pre><p>That's what most copy utilities are going to do anyway: create a new file and update the timestamps. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传