亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

golang 設置web請求狀態(tài)碼操作

 更新時間:2020年12月14日 14:37:36   作者:一名路過的小碼農(nóng)  
這篇文章主要介紹了golang 設置web請求狀態(tài)碼操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

package main
import (
 "net/http"
)
func main() {
 //路由處理綁定
 http.HandleFunc("/", Hander)
 //監(jiān)聽8080端口
 http.ListenAndServe(":8080", nil)
}
func Hander(w http.ResponseWriter, req *http.Request) {
 //設置 http請求狀態(tài)
 w.WriteHeader(500)
 //寫入頁面數(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
)

下面修改一下就是這個樣子

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

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

status.go使用了一個map集合定義了http的響應狀態(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對應的 狀態(tài)碼描述信息
// 返回空字符串表示狀態(tài)碼 unknown
func StatusText(code int) string {
 return statusText[code]
}

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

相關文章

  • Golang標準庫之errors包應用方式

    Golang標準庫之errors包應用方式

    Go語言的errors包提供了基礎的錯誤處理能力,允許通過errors.New創(chuàng)建自定義error對象,error在Go中是一個接口,通過實現(xiàn)Error方法來定義錯誤文本,對錯誤的比較通?;趯ο蟮刂?而非文本內(nèi)容,因此即使兩個錯誤文本相同
    2024-10-10
  • Go語言實現(xiàn)管理多個數(shù)據(jù)庫連接

    Go語言實現(xiàn)管理多個數(shù)據(jù)庫連接

    在軟件開發(fā)過程中,使用?MySQL、PostgreSQL?或其他數(shù)據(jù)庫是很常見的,由于配置和要求不同,管理這些連接可能具有挑戰(zhàn)性,下面就來和大家聊聊如何在Go中管理多個數(shù)據(jù)庫連接吧
    2023-10-10
  • 淺談Go語言不提供隱式數(shù)字轉(zhuǎn)換的原因

    淺談Go語言不提供隱式數(shù)字轉(zhuǎn)換的原因

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

    golang爬蟲colly?發(fā)送post請求

    本文主要介紹了golang爬蟲colly?發(fā)送post請求實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Go?常見設計模式之單例模式詳解

    Go?常見設計模式之單例模式詳解

    單例模式是設計模式中最簡單的一種模式,單例模式能夠確保無論對象被實例化多少次,全局都只有一個實例存在,在Go?語言有多種方式可以實現(xiàn)單例模式,所以我們今天就來一起學習下吧
    2023-07-07
  • GoFrame框架數(shù)據(jù)校驗之校驗對象校驗結(jié)構體

    GoFrame框架數(shù)據(jù)校驗之校驗對象校驗結(jié)構體

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

    Go語言中的sync包同步原語最新詳解

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

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

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

    使用Go語言實現(xiàn)心跳機制

    心跳最典型的應用場景是是探測服務是否存活,這篇文章主要來和大家介紹一下如何使用Go語言實現(xiàn)一個簡單的心跳程序,感興趣的可以了解下
    2024-01-01
  • Go語言流程控制詳情

    Go語言流程控制詳情

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

最新評論