英文:
Does goji work with Google App Engine/Go?
问题
我想使用Goji和Google App Engine/Go开发应用程序。
我从https://github.com/zenazn/goji复制并粘贴了示例代码,并将函数名从"main"更改为"init"。
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func init() {
goji.Get("/hello/:name", hello)
goji.Serve()
}
然后运行这个应用程序
# goapp serve
但是这个应用程序报错:
bad runtime process port ['']
2014/06/01 08:35:30.125553 listen tcp :8000: bind: address already in use
我该如何在GAE/Go中使用Goji?或者我不能在GAE/Go中使用Goji吗?
英文:
I would like to develop application with Goji and Google App Engine/Go.
I copied and pasted sample code from
https://github.com/zenazn/goji and changed func name from "main" to "init".
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func init() {
goji.Get("/hello/:name", hello)
goji.Serve()
}
and Run this application
# goapp serve
but this application says
bad runtime process port ['']
2014/06/01 08:35:30.125553 listen tcp :8000: bind: address already in use
How can I use Goji with GAE/Go? Or I cannot use Goji with GAE/Go?
答案1
得分: 5
init
函数是特殊的,它们会自动运行以初始化模块。通过在init函数中提供你的应用程序,你停止了代码的初始化过程(因为goji.Serve
永远不会终止)。可能发生的情况是某些内容依赖于运行时标志,而你在它们解析之前就运行了。
你可以编写一个单一的应用引擎处理程序,将服务转发给goji.DefaultMux
。我没有测试过,但是像下面这样的代码应该可以工作:
import (
"fmt"
"github.com/zenazn/goji"
"net/http"
)
func init() {
http.Handle("/", goji.DefaultMux)
... 在这里注册你的goji处理程序
}
英文:
init
functions are special, in that they're run automatically to initialize modules. By serving you app in an init function, you've stopped the initialization of your code mid-way (because goji.Serve
never terminates). Probably what's happening is that something depends on runtime flags, and you're running before they're parsed.
What you can do is write a single app engine handler that forwards the serving the goji.DefaultMux
. I've not tested it, but something like this should work:
import (
"fmt"
"github.com/zenazn/goji"
"net/http"
)
func init() {
http.Handle("/", goji.DefaultMux)
... Register your goji handlers here
}
答案2
得分: 2
这在我的开发服务器上运行良好。
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "首页")
}
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "你好,%s!", c.URLParams["name"])
}
func init() {
http.Handle("/", goji.DefaultMux)
goji.Get("/", index)
goji.Get("/hello/:name", hello)
}
英文:
This works fine in my dev server.
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "index page")
}
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func init() {
http.Handle("/", goji.DefaultMux)
goji.Get("/", index)
goji.Get("/hello/:name", hello)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论