Efficient implementation of randomly generated file of specified size?

agolangf · · 423 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey all, I have been messing around with trying to generate a file with random contents of size specified by user.</p> <p>Technically the example offered here would work: <a href="https://golang.org/pkg/crypto/rand/#pkg-examples" rel="nofollow">https://golang.org/pkg/crypto/rand/#pkg-examples</a></p> <p>But this would require me to maintain a buffer of size slice at a given size in memory. What if I wanted to generate a 4GB file for example, this isn&#39;t practical.</p> <p>I haven&#39;t found the most idiomatic way of hooking up crypt/rand to a bufio File Writer since the crypt Read function does not implement io.Reader (boo!)</p> <p>I know I must be missing the obvious, but can someone help me figure out a more efficient way of doing this? I must be doing this wrong!</p> <p><a href="https://play.golang.org/p/2z9g69gm3RH" rel="nofollow">https://play.golang.org/p/2z9g69gm3RH</a></p> <p>Basically, I want to do something analogous to</p> <pre><code>dd if=/dev/urandom of=file.txt bs=1024 count=10 </code></pre> <hr/>**评论:**<br/><br/>smcquay: <pre><p><a href="https://godoc.org/mcquay.me/trash" rel="nofollow">https://godoc.org/mcquay.me/trash</a></p> <p>I made that as an exercise. Each of those are io.Reader implementations. </p> <p>Salient bits:</p> <p><a href="https://s.mcquay.me/sm/trash/src/branch/master/trash.go#L16" rel="nofollow">https://s.mcquay.me/sm/trash/src/branch/master/trash.go#L16</a></p></pre>adawalli: <pre><p>awesome, I will give that a shot tomorrow - I think this is exactly what I need. Thanks!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

423 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传