Go Web 服务器无法正确处理/删除/模式化

标签 go

<分区>

我刚刚玩了一个google GO官方例子Writing Web Applications我试图添加删除页面的功能,但没有成功。原因是,如果您将 "/delete/" 作为参数传递给 http.HandleFunc() 函数,您总是会收到 404 Page not found。任何其他 "foobar" 字符串都按预期工作。

简化代码:

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)
}

重现步骤:

  1. 在浏览器中编译并调用http://localhost:8080/hello/world
  2. 输出为/hello/world
  3. 现在注释 http.HandleFunc("/hello/", handler) 并取消注释 http.HandleFunc("/delete/", handler)
  4. 在浏览器中编译并调用http://localhost:8080/delete/world
  5. 结果是 404 找不到页面,应该是 /delete/world

问题: "/delete/" 模式有什么特殊含义吗?这有任何技术原因还是只是一个错误?

最佳答案

这在这里工作得很好,它不可能在一种情况下工作而不在另一种情况下工作。您只是在两行之间将字符串 "hello" 更改为字符串 "delete"

我建议更仔细地再试一次。它必须是其他地方的细节。


真正的原因是没有检查ListenAndServe的错误结果。一个旧副本在后台运行,错误处理的缺乏使得它没有被察觉。浏览器从旧服务器获取结果。

关于Go Web 服务器无法正确处理/删除/模式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601629/

相关文章:

linux - 在 Go 中设置进程名称(如 `ps` 所示)

go - 带有 GO 的 Visual Studio Code - 多个主要声明(启动设置)

go - 如何听 0.0.0.0 :8080 with ListenTCP

http - 使用 MUX 包将查询参数传递给 Go HTTP 请求处理程序

post - Golang HTTP 发布错误 : connection refused

go - 用反射遍历结构,用反射创建新的指针类型 slice

php - http.newRequest 不发送发布数据

pointers - 具有混合返回/签名类型的 Go 函数

go - Go中的光标键终端输入

regex - 如何为要匹配的以下字符串编写正则表达式模式