appengine and main package

agolangf · 2017-11-01 20:00:04 · 476 次点击    
这是一个分享于 2017-11-01 20:00:04 的资源,其中的信息可能已经有所发展或是发生改变。

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.).

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 ?)


评论:

broady:

You can use a main package.

package main
import (
   "net/http"
   "google.golang.org/appengine"
)
func main() {
   http.HandleFunc(...)
   appengine.Main()
}
psankar:

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. https://groups.google.com/forum/#!topic/golang-nuts/Vce6234vk8E 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.

broady:

Done. Yes, sorry, updating docs is on a list of things to do. Thank you!

nstratos:

Use build tags on top of your main files like:

// +build appengine

and

// +build !appengine

Check golang/perf out.

psankar:

Thanks. But, I think I will go with the appengine.Main function recommended in the other comment.


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

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