Is there a static way to get current request/context in Go?

polaris · · 426 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I want my logging methods to output some sort of request ID. To do that I need to pass a request object (or some derivative) to the logging functions.</p> <p>This is all fine when logging from the request handlers, but is problematic when used from within library-style methods, as I don&#39;t wish those to be aware of any ongoing http-requests.<br/> Furthermore, those same library methods might be used in the future by console applications, and than the request-id would be replaced by some sort of worker-thread-id.</p> <p>Using a context solves the problem, but this means I will have to add a context parameter to all my methods, which is somewhat annoying.</p> <p>So, basically, what I need is some sort of static storage that is passed between method and goroutine calls.</p> <p>I&#39;m not sure there&#39;s anything like that in Go, so maybe my approach is totally off-base, in that case, I would be happy to hear what is a better approach to solve the above problem.</p> <p>Thanks,</p> <p>EDIT: Seems like context is the way to go.</p> <hr/>**评论:**<br/><br/>drvd: <pre><p>See <a href="http://stackoverflow.com/questions/43319129/is-there-a-static-way-to-get-current-request-context-in-go" rel="nofollow">http://stackoverflow.com/questions/43319129/is-there-a-static-way-to-get-current-request-context-in-go</a></p></pre>svarog: <pre><p>Yeah, that&#39;s the same question asked by me... trying to reach various populations ;)</p></pre>drvd: <pre><p>Why not ask on the golang-nuts mailing list?</p></pre>svarog: <pre><p>Good question, but I&#39;m pretty sure I already got my answer in the StackOverflow page.</p></pre>

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

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