go语言404 page not found
时间: 2023-07-07 14:07:03 浏览: 57
在Go语言中,你可以使用net/http包来创建一个Web服务器,并在服务器上设置404页面。以下是一个简单的示例:
```go
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
})
http.HandleFunc("/404", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404 Page Not Found")
})
http.ListenAndServe(":8080", nil)
}
```
在上面的示例中,我们首先定义了一个根路径的处理器来响应所有的请求。然后,我们定义了一个"/404"路径的处理器来处理404错误。该处理器返回一个HTTP状态码为404和一个自定义的404消息。
最后,我们使用`http.ListenAndServe`方法来启动我们的服务器。这个方法接受两个参数:监听地址和处理器。在这个例子中,我们将处理器设置为nil,这意味着我们将使用默认的处理器。
相关问题
用HTML写一个404错误提示网页
可以参考以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>404 Error - Page Not Found</title>
<style>
body {
background-color: #f7f7f7;
font-family: Arial, sans-serif;
}
h1 {
font-size: 3em;
color: #555;
text-align: center;
margin-top: 100px;
}
p {
font-size: 1.5em;
color: #777;
text-align: center;
margin-top: 50px;
}
button {
display: block;
margin: 50px auto 0;
padding: 10px 20px;
font-size: 1.2em;
background-color: #555;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #333;
}
</style>
</head>
<body>
<h1>404 Error - Page Not Found</h1>
<p>The page you are looking for could not be found.</p>
<button onclick="window.history.back()">Go Back</button>
</body>
</html>
```
这个页面会显示一个大标题“404 Error - Page Not Found”和一个提示信息“ The page you are looking for could not be found.”。页面中央有一个“Go Back”按钮,点击可以返回上一页。
用html写一个404报错页面
可以参考以下示例代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 Not Found</title>
<style>
body {
background-color: #f1f1f1;
font-family: sans-serif;
}
h1 {
text-align: center;
margin-top: 100px;
}
p {
text-align: center;
font-size: 18px;
margin-top: 50px;
}
a {
display: block;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>404 Not Found</h1>
<p>The page you requested could not be found.</p>
<a href="/">Go back to homepage</a>
</body>
</html>
```
这个页面会显示一个大标题 "404 Not Found",一段文本说明页面没有找到,以及一个链接返回到网站主页。你可以根据需要自定义样式和文本。