假设我们有:
http.HandleFunc("/smth", smthPage)
http.HandleFunc("/", homePage)
当用户尝试错误的 URL 时,会看到一个简单的“404页面未找到”。我怎样才能返回一个自定义页面的情况?
关于大猩猩的最新消息
接受的答案是确定的那些使用纯净的网络/http 包。
如果你使用 gorilla/mux,你应该使用这样的东西:
func main() {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(notFound)
}
并根据需要实现 func notFound(w http.ResponseWriter, r *http.Request)
。