为什么路由无法解析?

huangapple go评论105阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2014年2月8日 07:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/21639585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定