Writing to multiple outputs (io.Multiwriter?)

xuanbao · · 584 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Say I want to take data coming in as a http request and simultaneously write it to multiple places (e. g. to file, to s3, to some cache etc). </p> <p>What is the best pattern in go for doing that? </p> <p>Would be nice to avoid loading the data into memory in full as it can be quite large at times</p> <hr/>**评论:**<br/><br/>The_Sly_Marbo: <pre><p>As your title suggests, you want to use <code>io.MultiWriter</code> to collect your outputs together, then use <code>io.Copy</code> to copy from you input (the request body) into the multi writer.</p></pre>joeyGibson: <pre><p><code>io.MultiWriter</code> is the way to go. This is a snippet of some code I wrote recently. I needed to copy a file into an HTTP form to <code>POST</code>, but I also needed to compute a SHA256 hash of the file. <code>MultiWriter</code> solved it nicely. (Note: In the actual code, I&#39;m not ignoring the <code>error</code> returns. I removed them for this post, for brevity.)</p> <pre><code>sha := sha256.New() file, _ := os.Open(pathName) defer file.Close() var b bytes.Buffer w := multipart.NewWriter(&amp;b) _, fileName := path.Split(pathName) fw, _ := w.CreateFormFile(&#34;file&#34;, fileName) // Create the MultiWriter to write to both writers multiWriter := io.MultiWriter(sha, fw) // Efficiently copy the file to both writers io.Copy(multiWriter, file) hash := sha.Sum(nil) logger.Infof(&#34;%s -&gt; %x&#34;, pathName, hash) </code></pre></pre>

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

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