<p>I am evaluating appengine for a golang project. It is a simple REST server with a bunch of endpoints. I am currently deploying it in AWS, in an EC2 instance. I have written a systemd script file which will take care of launching the webserver as a systemd service (so that it is always up etc.).</p>
<p>I am evaluating appengine and I see that we need to change the package main to something else, as appengine needs to have its own main package. This is kind of annoying as now I cannot easily test in my dev laptop or have the same code across my EC2 instance and appengine. So, are there any tricks to switch the main package automatically when I am deploying to appengine ? (Some kind of pre-push patch or something ?)</p>
<hr/>**评论:**<br/><br/>broady: <pre><p>You can use a main package.</p>
<pre><code>package main
import (
"net/http"
"google.golang.org/appengine"
)
func main() {
http.HandleFunc(...)
appengine.Main()
}
</code></pre></pre>psankar: <pre><p>This is brilliant. Thanks. I never knew that appengine can live without a Main package. This is not covered even in the official go-appengine docs.
I posted this query in the golang nuts list also. <a href="https://groups.google.com/forum/#!topic/golang-nuts/Vce6234vk8E" rel="nofollow">https://groups.google.com/forum/#!topic/golang-nuts/Vce6234vk8E</a> I believe that it will be nice if you could reply there also, so that it will be beneficial to many. If you want, I could just refer to this thread and send the mail myself too. Thanks.</p></pre>broady: <pre><p>Done. Yes, sorry, updating docs is on a list of things to do. Thank you!</p></pre>nstratos: <pre><p>Use <a href="https://golang.org/pkg/go/build/" rel="nofollow">build</a> <a href="https://dave.cheney.net/2013/10/12/how-to-use-conditional-compilation-with-the-go-build-tool" rel="nofollow">tags</a> on top of your main files like: </p>
<pre><code>// +build appengine
</code></pre>
<p>and</p>
<pre><code>// +build !appengine
</code></pre>
<p>Check <a href="https://github.com/golang/perf/blob/cab923dd6b1a9c8bc6bef50023f8efc9d9508b42/storage/appengine/app.go" rel="nofollow">golang/perf</a> out.</p></pre>psankar: <pre><p>Thanks. But, I think I will go with the appengine.Main function recommended in the other comment.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传