How can I solve problem of 404 page display?

huangapple go评论84阅读模式
英文:

How can I solve problem of 404 page display?

问题

我有一个包含404错误页面的主页函数:

// 处理主页
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("它还活着吗?"))
// 创建404页面
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
}

但是当我访问无法访问的页面(例如'127.0.0.1:4000/nothing'),我会同时得到两部分内容 - "它还活着吗?"和"404页面未找到"的消息:
How can I solve problem of 404 page display?
那么,我该怎么解决这个问题?

英文:

I have the function for the main page, which contains 'if' part for the 404 error page:

// Обработчик главной страницы
func home(w http.ResponseWriter, r *http.Request)  {
	w.Write([]byte("Is it alive?"))
	// Создание страницы 404
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
}

But when I go to the unable page ('127.0.0.1:4000/nothing' for example), I get back both parts - and message "is it alive?" and "404 page not found":
How can I solve problem of 404 page display?
So, what can I do to solve it?

答案1

得分: 1

func home(w http.ResponseWriter, r *http.Request)  {
    // 创建404页面
    if r.URL.Path != "/" {
        http.NotFound(w, r)
        return
    }

    w.Write([]byte("它还活着吗?"))
}
英文:
func home(w http.ResponseWriter, r *http.Request)  {
    // Создание страницы 404
    if r.URL.Path != "/" {
        http.NotFound(w, r)
        return
    }

    w.Write([]byte("Is it alive?"))
}

huangapple
  • 本文由 发表于 2021年7月28日 18:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/68558590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定