duannan4486 2018-05-17 21:26
浏览 88
已采纳

REST端在1.6之后的go版本中返回404

I upgraded from 1.6 golang to 1.9 (and 1.10) and all REST calls are now returning 404. They were working fine with 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)
...}
  • 写回答

1条回答 默认 最新

  • dt614037527 2018-05-18 00:59
    关注

    The main function copies the mux value when calling the TableHandler function. The TableHandler function modifies the copy. This value is discarded on return from the TableHandler function. The result is the handler is not registered in main()'s mux.

    The fix is to change the TableHandler argument type from http.ServeMux to *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)
    ...}
    

    The application stopped working because of a change to http.ServeMux in 2016. This change exposed the problem in the application. Copying http.ServeMux values was never supported. The go vet command prints a warning when http.ServeMux values are copied.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式