英文:
go -mux, why the route doesn't resolve?
问题
我正在尝试在GAE上部署我的第一个Go应用程序。由于某些原因,产品处理程序无法解析,导致我收到404错误。我是否漏掉了什么?
package test
import "github.com/gorilla/mux"
import (
"fmt"
"net/http"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/products", ProductsHandler)
http.Handle("/", r)
e := http.ListenAndServe(":8080", r)
if e != nil {
println(e.Error())
}
}
func ProductsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, you!")
}
英文:
I'm trying to deploy my first golang app on GAE. For some reasons the products handler doesn't resolve and I get 404 errors. Am I missing something ?
package test
import "github.com/gorilla/mux"
import (
"fmt"
"net/http"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/products", ProductsHandler)
http.Handle("/", r)
e := http.ListenAndServe(":8080", r)
if e != nil {
println(e.Error())
}
}
func ProductsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, you!")
}
答案1
得分: 3
继续使用AppEngine将自动监听正确的端口并提供http.DefaultServeMux
。将您的main
函数更改为init
并删除服务逻辑,然后您就可以开始了。
阅读开始使用请求和HTTP部分以获取更多详细信息。
英文:
Go on AppEngine will automatically listen on the correct port and serve the http.DefaultServeMux
. Change your main
function to an init
and remove the serve logic and you should be all set.
Read through the Getting Started section on Requests and HTTP for more detail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论