golang 設(shè)置web請(qǐng)求狀態(tài)碼操作
我就廢話不多說(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) //寫(xiě)入頁(yè)面數(shù)據(jù) w.Write([]byte("xiaochuan")) }
你也可以用http 包里面的常量 我這邊直接寫(xiě)數(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) //寫(xiě)入頁(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ò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Golang標(biāo)準(zhǔn)庫(kù)之errors包應(yīng)用方式
Go語(yǔ)言的errors包提供了基礎(chǔ)的錯(cuò)誤處理能力,允許通過(guò)errors.New創(chuàng)建自定義error對(duì)象,error在Go中是一個(gè)接口,通過(guò)實(shí)現(xiàn)Error方法來(lái)定義錯(cuò)誤文本,對(duì)錯(cuò)誤的比較通常基于對(duì)象地址,而非文本內(nèi)容,因此即使兩個(gè)錯(cuò)誤文本相同2024-10-10Go語(yǔ)言實(shí)現(xiàn)管理多個(gè)數(shù)據(jù)庫(kù)連接
在軟件開(kāi)發(fā)過(guò)程中,使用?MySQL、PostgreSQL?或其他數(shù)據(jù)庫(kù)是很常見(jiàn)的,由于配置和要求不同,管理這些連接可能具有挑戰(zhàn)性,下面就來(lái)和大家聊聊如何在Go中管理多個(gè)數(shù)據(jù)庫(kù)連接吧2023-10-10淺談Go語(yǔ)言不提供隱式數(shù)字轉(zhuǎn)換的原因
本文主要介紹了淺談Go語(yǔ)言不提供隱式數(shù)字轉(zhuǎn)換的原因,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03golang爬蟲(chóng)colly?發(fā)送post請(qǐng)求
本文主要介紹了golang爬蟲(chóng)colly?發(fā)送post請(qǐng)求實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Go?常見(jiàn)設(shè)計(jì)模式之單例模式詳解
單例模式是設(shè)計(jì)模式中最簡(jiǎn)單的一種模式,單例模式能夠確保無(wú)論對(duì)象被實(shí)例化多少次,全局都只有一個(gè)實(shí)例存在,在Go?語(yǔ)言有多種方式可以實(shí)現(xiàn)單例模式,所以我們今天就來(lái)一起學(xué)習(xí)下吧2023-07-07GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)對(duì)象校驗(yàn)結(jié)構(gòu)體
這篇文章主要為大家介紹了GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)對(duì)象校驗(yàn)結(jié)構(gòu)體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06使用Go語(yǔ)言實(shí)現(xiàn)心跳機(jī)制
心跳最典型的應(yīng)用場(chǎng)景是是探測(cè)服務(wù)是否存活,這篇文章主要來(lái)和大家介紹一下如何使用Go語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的心跳程序,感興趣的可以了解下2024-01-01