欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年12月14日 14:37:36   作者:一名路過(guò)的小碼農(nóng)  
這篇文章主要介紹了golang 設(shè)置web請(qǐng)求狀態(tài)碼操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lá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)用方式

    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-10
  • Go語(yǔ)言實(shí)現(xiàn)管理多個(gè)數(shù)據(jù)庫(kù)連接

    Go語(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)換的原因

    本文主要介紹了淺談Go語(yǔ)言不提供隱式數(shù)字轉(zhuǎn)換的原因,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • golang爬蟲(chóng)colly?發(fā)送post請(qǐng)求

    golang爬蟲(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-07
  • Go?常見(jiàn)設(shè)計(jì)模式之單例模式詳解

    Go?常見(jiàn)設(shè)計(jì)模式之單例模式詳解

    單例模式是設(shè)計(jì)模式中最簡(jiǎn)單的一種模式,單例模式能夠確保無(wú)論對(duì)象被實(shí)例化多少次,全局都只有一個(gè)實(shí)例存在,在Go?語(yǔ)言有多種方式可以實(shí)現(xiàn)單例模式,所以我們今天就來(lái)一起學(xué)習(xí)下吧
    2023-07-07
  • GoFrame框架數(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)體

    這篇文章主要為大家介紹了GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)對(duì)象校驗(yàn)結(jié)構(gòu)體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Go語(yǔ)言中的sync包同步原語(yǔ)最新詳解

    Go語(yǔ)言中的sync包同步原語(yǔ)最新詳解

    Go語(yǔ)言在sync包中提供了一套多才多藝的同步機(jī)制,以及用于管理對(duì)共享資源的并發(fā)訪問(wèn)的原子操作,了解這些工具并為您的并發(fā)需求選擇合適的工具是編寫(xiě)高效可靠的并發(fā)Go程序的關(guān)鍵,這篇文章主要介紹了Go語(yǔ)言中的`sync`包同步原語(yǔ),需要的朋友可以參考下
    2023-12-12
  • golang中隨機(jī)數(shù)rand的使用

    golang中隨機(jī)數(shù)rand的使用

    本文主要介紹了golang中隨機(jī)數(shù)rand的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 使用Go語(yǔ)言實(shí)現(xiàn)心跳機(jī)制

    使用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
  • Go語(yǔ)言流程控制詳情

    Go語(yǔ)言流程控制詳情

    這篇文章主要介紹了Go語(yǔ)言流程控制詳情,流程控制包含分三大類:條件判斷,循環(huán)控制和無(wú)條件跳轉(zhuǎn)。下面關(guān)于更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-03-03

最新評(píng)論