Search code examples
gohttp-status-code-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 it looks like: So, what can I do to solve it?


Solution

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