<p>I am doing something similar with a global variable by setting an infinite loop that breaks when the handler changes the global variable.</p>
<p>It seems super hacky though, and I would like to more cleanly do this if at all possible. </p>
<p>The problem seems to be that the handler (not done as a closure) isn't really "aware" of the server, and I don't really know how to make it aware of the server since the handler has to be defined by: <code>handler func(ResponseWriter, *Request))</code> <em>ie</em> there doesn't seem to be room to pass the server in to the handler as a variable. Similarly the handler doesn't return anything.</p>
<p>Hopefully that makes sense. If not, I'd be glad to clear anything up.</p>
<hr/>**评论:**<br/><br/>everdev: <pre><p>It seems odd to shutdown a server after an HTTP request to a specific handler, but in any event, interfaces can have receivers:</p>
<pre><code>type Env struct {
Server *http.Server
// any other environment variables
}
func (env *Env) ShutdownHandler(w http.ResponseWriter, r *http.Request) {
env.Server.Shutdown()
}
</code></pre></pre>qspec02: <pre><p>Thanks. That's a far easier solution than I was expecting. I was so hung up on finding something in the net/http package.</p>
<p>Thanks again!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传