REST end returns 404 in go version 1.6 之后
REST end returns 404 in go version after 1.6
我从 1.6 golang 升级到 1.9(和 1.10),所有 REST 调用现在都返回 404。它们在 1.6 上运行良好。
main.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
函数的 return 上被丢弃。结果是处理程序未在 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
值。 go vet
命令在复制 http.ServeMux
值时打印警告。
我从 1.6 golang 升级到 1.9(和 1.10),所有 REST 调用现在都返回 404。它们在 1.6 上运行良好。
main.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
函数的 return 上被丢弃。结果是处理程序未在 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
值。 go vet
命令在复制 http.ServeMux
值时打印警告。