濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > golang 設(shè)置web請(qǐng)求狀態(tài)碼操作

golang 設(shè)置web請(qǐng)求狀態(tài)碼操作

熱門標(biāo)簽:利用地圖標(biāo)注位置 赤峰電銷 杭州人工智能電銷機(jī)器人費(fèi)用 江蘇呼叫中心外呼系統(tǒng)有效果嗎 官渡電銷外呼管理系統(tǒng)怎么收費(fèi) 貴州電話智能外呼系統(tǒng) 400開頭電話怎樣申請(qǐng) 谷歌美發(fā)店地圖標(biāo)注 地圖區(qū)域圖標(biāo)注后導(dǎo)出

我就廢話不多說(shuō)了,大家還是直接看代碼吧~

package main
import (
 "net/http"
)
func main() {
 //路由處理綁定
 http.HandleFunc("/", Hander)
 //監(jiān)聽(tīng)8080端口
 http.ListenAndServe(":8080", nil)
}
func Hander(w http.ResponseWriter, req *http.Request) {
 //設(shè)置 http請(qǐng)求狀態(tài)
 w.WriteHeader(500)
 //寫入頁(yè)面數(shù)據(jù)
 w.Write([]byte("xiaochuan"))
}

你也可以用http 包里面的常量 我這邊直接寫數(shù)字方便理解而已

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
)

下面修改一下就是這個(gè)樣子

package main
import (
 "net/http"
)
func main() {
 //路由處理綁定
 http.HandleFunc("/", Hander)
 //監(jiān)聽(tīng)8080端口
 http.ListenAndServe(":8080", nil)
}
func Hander(w http.ResponseWriter, req *http.Request) {
 //設(shè)置 http請(qǐng)求狀態(tài) 為500
 w.WriteHeader(http.StatusInternalServerError)
 //寫入頁(yè)面數(shù)據(jù)
 w.Write([]byte("xiaochuan"))
}

補(bǔ)充:go status.go 狀態(tài)碼定義

status.go使用了一個(gè)map集合定義了http的響應(yīng)狀態(tài)碼

具體的參考如下

// 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對(duì)應(yīng)的 狀態(tài)碼描述信息
// 返回空字符串表示狀態(tài)碼 unknown
func StatusText(code int) string {
 return statusText[code]
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Golang發(fā)送http GET請(qǐng)求的示例代碼
  • golang使用http client發(fā)起get和post請(qǐng)求示例
  • golang http請(qǐng)求封裝代碼
  • 詳解golang開發(fā)中http請(qǐng)求redirect的問(wèn)題
  • golang實(shí)現(xiàn)各種情況的get請(qǐng)求操作

標(biāo)簽:宜春 鷹潭 河池 武漢 黔西 保定 松原 泰安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang 設(shè)置web請(qǐng)求狀態(tài)碼操作》,本文關(guān)鍵詞  golang,設(shè)置,web,請(qǐng)求,狀態(tài),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang 設(shè)置web請(qǐng)求狀態(tài)碼操作》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于golang 設(shè)置web請(qǐng)求狀態(tài)碼操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    麻栗坡县| 堆龙德庆县| 凯里市| 炎陵县| 临沧市| 法库县| 紫金县| 乌兰浩特市| 罗城| 禹州市| 高尔夫| 镇平县| 通河县| 普兰店市| 铁力市| 威海市| 峡江县| 慈利县| 蒲城县| 沙河市| 墨竹工卡县| 壤塘县| 漾濞| 景东| 定陶县| 嘉荫县| 秦皇岛市| 沂水县| 岐山县| 榆中县| 凌海市| 甘孜县| 钟祥市| 砚山县| 靖边县| 常德市| 逊克县| 赞皇县| 上林县| 邯郸县| 定结县|