golang 设置web请求状态码操作
作者:一名路过的小码农 时间:2024-05-22 10:30:12
我就废话不多说了,大家还是直接看代码吧~
package main
import (
"net/http"
)
func main() {
//路由处理绑定
http.HandleFunc("/", Hander)
//监听8080端口
http.ListenAndServe(":8080", nil)
}
func Hander(w http.ResponseWriter, req *http.Request) {
//设置 http请求状态
w.WriteHeader(500)
//写入页面数据
w.Write([]byte("xiaochuan"))
}
你也可以用http 包里面的常量 我这边直接写数字方便理解而已
const (
StatusContinue = 100
StatusSwitchingProtocols = 101
StatusOK = 200
StatusCreated = 201
StatusAccepted = 202
StatusNonAuthoritativeInfo = 203
StatusNoContent = 204
StatusResetContent = 205
StatusPartialContent = 206
StatusMultipleChoices = 300
StatusMovedPermanently = 301
StatusFound = 302
StatusSeeOther = 303
StatusNotModified = 304
StatusUseProxy = 305
StatusTemporaryRedirect = 307
StatusBadRequest = 400
StatusUnauthorized = 401
StatusPaymentRequired = 402
StatusForbidden = 403
StatusNotFound = 404
StatusMethodNotAllowed = 405
StatusNotAcceptable = 406
StatusProxyAuthRequired = 407
StatusRequestTimeout = 408
StatusConflict = 409
StatusGone = 410
StatusLengthRequired = 411
StatusPreconditionFailed = 412
StatusRequestEntityTooLarge = 413
StatusRequestURITooLong = 414
StatusUnsupportedMediaType = 415
StatusRequestedRangeNotSatisfiable = 416
StatusExpectationFailed = 417
StatusTeapot = 418
StatusInternalServerError = 500
StatusNotImplemented = 501
StatusBadGateway = 502
StatusServiceUnavailable = 503
StatusGatewayTimeout = 504
StatusHTTPVersionNotSupported = 505
// New HTTP status codes from RFC 6585. Not exported yet in Go 1.1.
// See discussion at https://codereview.appspot.com/7678043/
statusPreconditionRequired = 428
statusTooManyRequests = 429
statusRequestHeaderFieldsTooLarge = 431
statusNetworkAuthenticationRequired = 511
)
下面修改一下就是这个样子
package main
import (
"net/http"
)
func main() {
//路由处理绑定
http.HandleFunc("/", Hander)
//监听8080端口
http.ListenAndServe(":8080", nil)
}
func Hander(w http.ResponseWriter, req *http.Request) {
//设置 http请求状态 为500
w.WriteHeader(http.StatusInternalServerError)
//写入页面数据
w.Write([]byte("xiaochuan"))
}
补充:go status.go 状态码定义
status.go使用了一个map集合定义了http的响应状态码
具体的参考如下
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
// HTTP status codes, defined in RFC 2616.
const (
StatusContinue = 100
StatusSwitchingProtocols = 101
StatusOK = 200
StatusCreated = 201
StatusAccepted = 202
StatusNonAuthoritativeInfo = 203
StatusNoContent = 204
StatusResetContent = 205
StatusPartialContent = 206
StatusMultipleChoices = 300
StatusMovedPermanently = 301
StatusFound = 302
StatusSeeOther = 303
StatusNotModified = 304
StatusUseProxy = 305
StatusTemporaryRedirect = 307
StatusBadRequest = 400
StatusUnauthorized = 401
StatusPaymentRequired = 402
StatusForbidden = 403
StatusNotFound = 404
StatusMethodNotAllowed = 405
StatusNotAcceptable = 406
StatusProxyAuthRequired = 407
StatusRequestTimeout = 408
StatusConflict = 409
StatusGone = 410
StatusLengthRequired = 411
StatusPreconditionFailed = 412
StatusRequestEntityTooLarge = 413
StatusRequestURITooLong = 414
StatusUnsupportedMediaType = 415
StatusRequestedRangeNotSatisfiable = 416
StatusExpectationFailed = 417
StatusTeapot = 418
StatusPreconditionRequired = 428
StatusTooManyRequests = 429
StatusRequestHeaderFieldsTooLarge = 431
StatusUnavailableForLegalReasons = 451
StatusInternalServerError = 500
StatusNotImplemented = 501
StatusBadGateway = 502
StatusServiceUnavailable = 503
StatusGatewayTimeout = 504
StatusHTTPVersionNotSupported = 505
StatusNetworkAuthenticationRequired = 511
)
var statusText = map[int]string{
StatusContinue: "Continue",
StatusSwitchingProtocols: "Switching Protocols",
StatusOK: "OK",
StatusCreated: "Created",
StatusAccepted: "Accepted",
StatusNonAuthoritativeInfo: "Non-Authoritative Information",
StatusNoContent: "No Content",
StatusResetContent: "Reset Content",
StatusPartialContent: "Partial Content",
StatusMultipleChoices: "Multiple Choices",
StatusMovedPermanently: "Moved Permanently",
StatusFound: "Found",
StatusSeeOther: "See Other",
StatusNotModified: "Not Modified",
StatusUseProxy: "Use Proxy",
StatusTemporaryRedirect: "Temporary Redirect",
StatusBadRequest: "Bad Request",
StatusUnauthorized: "Unauthorized",
StatusPaymentRequired: "Payment Required",
StatusForbidden: "Forbidden",
StatusNotFound: "Not Found",
StatusMethodNotAllowed: "Method Not Allowed",
StatusNotAcceptable: "Not Acceptable",
StatusProxyAuthRequired: "Proxy Authentication Required",
StatusRequestTimeout: "Request Timeout",
StatusConflict: "Conflict",
StatusGone: "Gone",
StatusLengthRequired: "Length Required",
StatusPreconditionFailed: "Precondition Failed",
StatusRequestEntityTooLarge: "Request Entity Too Large",
StatusRequestURITooLong: "Request URI Too Long",
StatusUnsupportedMediaType: "Unsupported Media Type",
StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
StatusExpectationFailed: "Expectation Failed",
StatusTeapot: "I'm a teapot",
StatusPreconditionRequired: "Precondition Required",
StatusTooManyRequests: "Too Many Requests",
StatusRequestHeaderFieldsTooLarge: "Request Header Fields Too Large",
StatusUnavailableForLegalReasons: "Unavailable For Legal Reasons",
StatusInternalServerError: "Internal Server Error",
StatusNotImplemented: "Not Implemented",
StatusBadGateway: "Bad Gateway",
StatusServiceUnavailable: "Service Unavailable",
StatusGatewayTimeout: "Gateway Timeout",
StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
StatusNetworkAuthenticationRequired: "Network Authentication Required",
}
// 返回httpcode对应的 状态码描述信息
// 返回空字符串表示状态码 unknown
func StatusText(code int) string {
return statusText[code]
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://xiaochuan.blog.csdn.net/article/details/54575001
标签:golang,web请求,状态码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django实现上传图片功能
2022-01-06 13:57:44
![](https://img.aspxhome.com/file/2023/4/66364_0s.jpg)
使用Selenium实现微博爬虫(预登录、展开全文、翻页)
2022-07-09 11:00:18
![](https://img.aspxhome.com/file/2023/2/107562_0s.jpg)
Python实现自动签到脚本的示例代码
2021-07-07 14:08:37
![](https://img.aspxhome.com/file/2023/0/90670_0s.png)
使用python获取csv文本的某行或某列数据的实例
2021-06-13 14:11:20
![](https://img.aspxhome.com/file/2023/9/85329_0s.jpg)
理理Vue细节(推荐)
2024-05-28 15:53:07
一小时学会TensorFlow2之自定义层
2021-12-22 18:00:11
![](https://img.aspxhome.com/file/2023/5/128735_0s.jpg)
白 刃之战:PHP vs. ASP.NET(节选)-架构比较
2023-11-15 12:31:22
Vue工程模板文件 webpack打包配置方法
2024-05-10 14:15:40
Python区块链Creating Miners教程
2021-03-25 11:39:27
pycharm进入时每次都是insert模式的解决方式
2023-08-09 09:52:16
![](https://img.aspxhome.com/file/2023/6/62906_0s.jpg)
vue 实现特定条件下绑定事件
2023-07-02 16:39:42
讲解MaxDB数据库和MySQL数据库的主要差别
2009-01-04 13:04:00
Vue动态路由缓存不相互影响的解决办法
2024-05-13 09:44:16
在EF中使用MySQL的方法及常见问题
2024-01-28 11:22:03
JavaScript实现计算圆周率到小数点后100位的方法示例
2024-02-27 02:38:58
详解Python中的format格式化函数的使用方法
2023-04-12 22:47:05
![](https://img.aspxhome.com/file/2023/2/77612_0s.png)
asp如何用ADO批量更新记录?
2010-06-10 18:42:00
Go语言实现超时的三种方法实例
2023-06-22 18:32:43
浅谈ThinkPHP5.0版本和ThinkPHP3.2版本的区别
2023-09-09 23:41:04
MySQL 数据库 like 语句通配符模糊查询小结
2024-01-20 03:41:30
![](https://img.aspxhome.com/file/2023/1/109811_0s.gif)