douming4359 2013-09-03 21:14
浏览 9
已采纳

Go Web服务器无法正确处理/ delete /模式[关闭]

I just played with a google GO official example Writing Web Applications I tried to add a functionality to delete pages and it has not worked. The reason is that if you pass "/delete/" as a parameter to http.HandleFunc() function you get always 404 Page not found. Any other "foobar" string works as expected.

Simplified code:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%s", r.URL.Path)
}

func main() {
    http.HandleFunc("/hello/", handler)
    //http.HandleFunc("/delete/", handler)
    http.ListenAndServe(":8080", nil)
}

Steps to reproduce:

  1. Compile and call http://localhost:8080/hello/world from a browser
  2. Output is /hello/world
  3. Now comment http.HandleFunc("/hello/", handler) and uncomment http.HandleFunc("/delete/", handler)
  4. Compile and call http://localhost:8080/delete/world from a browser
  5. Result is 404 page not found, expected /delete/world

Question: Is there any special meaning for "/delete/" pattern? Is there any technical reason for that or is it just a bug?

  • 写回答

1条回答 默认 最新

  • dongzhong1929 2013-09-03 21:28
    关注

    This works fine here, and it cannot possibly work in one situation and not the other. You're just changing the string "hello" for the string "delete" between the two lines.

    I suggest trying again more carefully. It must be a detail elsewhere.


    The real reason was not checking the error result of ListenAndServe. An old copy was running in background, and the lack of error handling made it go unperceived. The browser was getting results from an old server.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c