我从 1.6 golang 升级到 1.9(和 1.10),所有 REST 调用现在都返回 404。它们在 1.6 上运行良好。
主.go
import ( "net/http" )
func main() {
mux := http.NewServeMux()
handlers.TableHandler(*mux)
}
table_handler.go
func TableHandler(mux http.ServeMux) {
mux.HandleFunc("/gpdp/v1/prices/", func(w http.ResponseWriter, r *http.Request)
log.Println("request for prices: ", r.Method, r.Body)
...}
最佳答案
main
函数在调用TableHandler
函数时复制mux
值。 TableHandler
函数修改副本。该值在 TableHandler
函数返回时被丢弃。结果是处理程序未在 main()
的 mux 中注册。
修复方法是将 TableHandler
参数类型从 http.ServeMux
更改为 *http.ServeMux
。
import ( "net/http" )
func main() {
mux := http.NewServeMux()
handlers.TableHandler(mux) // <--- pass pointer here
}
func TableHandler(mux *http.ServeMux) { // <--- declare arg as pointer
mux.HandleFunc("/gpdp/v1/prices/", func(w http.ResponseWriter, r *http.Request)
log.Println("request for prices: ", r.Method, r.Body)
...}
应用程序因 change to http.ServeMux in 2016 而停止工作.此更改暴露了应用程序中的问题。从未支持复制 http.ServeMux
值。当复制 http.ServeMux
值时,go vet
命令会打印一条警告。
关于go - 1.6之后go版本REST端返回404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50400760/