go - 1.6之后go版本REST端返回404

标签 go http-status-code-404 upgrade

我从 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/

相关文章:

javascript - 如果向第三方 API 发出错误请求,Node JS Express 如何发送 404 错误?

c# - IIS 显示 404 但文件存在

google-play - 如何在没有旧证书和私钥的情况下在Google Play中升级Android应用

haskell - 将 haskell 平台更新到最新版本的最流畅方法是什么?

MongoDB in Go (golang) with mgo : How do I update a record, 找出更新是否成功并在单个原子操作中获取数据?

pointers - 如何在类型断言后调用带有指针接收器的方法?

go - 从 Go 中的可变参数创建带有可选字段的类型

go - 如何使用 Go 连接器将 Tarantool 选择结果序列化为就绪结构

html - Servlet返回“HTTP状态404请求的资源(/Servlet)不可用”

upgrade - 将 artifactory 6.x 升级到 7.12/最新版本(中间步骤(7.8/7.5)?)?