转到 MUX Controller 返回 404

标签 go mux

我一定遗漏了一些非常明显的东西,但我已经创建了一个 MUX 路由 Controller 并且服务器返回 404。运行以下命令:

package main

import (
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/mux"
)

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/hi", SayHi)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func SayHi(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hi")
}

访问:http://localhost:8080/hi我得到了 404。

我做错了什么?

最佳答案

只需将router 变量作为第二个参数传递给http.ListenAndServe()而不是 nil

关于转到 MUX Controller 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102625/

相关文章:

go - Golang 中的加权随机数

go - 从不同的包golang实现接口(interface)

go - 使用要在运行时填充的字符串 slice

google-app-engine - GAE Golang Gorilla mux - 404 页面未找到

go - gorilla Mux相同端点多个查询参数

go - 短变量声明和 "variable declared and not used"错误

go - 在 Go 中间接更改结构中的值

go - 如何将嵌套结构传递给参数

Go 和 Gorilla Mux NotFoundHandler 不工作

go - 在 Gorilla Mux 中嵌套子路由器