gpt4 book ai didi

google-app-engine - goji 可以与 Google App Engine/Go 一起使用吗?

转载 作者:IT王子 更新时间:2023-10-29 01:26:07 26 4
gpt4 key购买 nike

我想用 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

如何将 Goji 与 GAE/Go 一起使用?或者我不能将 Goji 与 GAE/Go 一起使用?

最佳答案

init 函数很特殊,因为它们会自动运行以初始化模块。通过在 init 函数中为您的应用程序提供服务,您已经中途停止了代码的初始化(因为 goji.Serve 永远不会终止)。可能发生的事情是某些东西取决于运行时标志,而您在它们被解析之前就已经运行了。

您可以编写一个应用引擎处理程序,将服务转发给 goji.DefaultMux。我没有测试过,但像这样的东西应该可以工作:

import (
"fmt"
"github.com/zenazn/goji"
"net/http"
)

func init() {
http.Handle("/", goji.DefaultMux)
... Register your goji handlers here
}

关于google-app-engine - goji 可以与 Google App Engine/Go 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23978181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com