最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)聽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)聽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ò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • Golang Mutex實(shí)現(xiàn)互斥的具體方法

    Golang Mutex實(shí)現(xiàn)互斥的具體方法

    Mutex是Golang常見(jiàn)的并發(fā)原語(yǔ),在開發(fā)過(guò)程中經(jīng)常使用到,本文主要介紹了Golang Mutex實(shí)現(xiàn)互斥的具體方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-04-04
  • Go語(yǔ)言的GOPATH與工作目錄詳解

    Go語(yǔ)言的GOPATH與工作目錄詳解

    這篇文章主要介紹了Go語(yǔ)言的GOPATH與工作目錄詳解,本文詳細(xì)講解了GOPATH設(shè)置、應(yīng)用目錄結(jié)構(gòu)、編譯應(yīng)用等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問(wèn)題解析

    Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露問(wèn)題解析

    這篇文章主要介紹了Go?中?time.After?可能導(dǎo)致的內(nèi)存泄露,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Go語(yǔ)言學(xué)習(xí)之結(jié)構(gòu)體和方法使用詳解

    Go語(yǔ)言學(xué)習(xí)之結(jié)構(gòu)體和方法使用詳解

    這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言中結(jié)構(gòu)體和方法的使用,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Go語(yǔ)言有一定的幫助,需要的可以參考一下
    2022-04-04
  • 一文詳解Golang如何解決內(nèi)存溢出

    一文詳解Golang如何解決內(nèi)存溢出

    內(nèi)存溢出是指程序在運(yùn)行時(shí)超出了分配給它的內(nèi)存限制,從而導(dǎo)致程序異常或崩潰的現(xiàn)象,本文主要為大家介紹了Golang解決內(nèi)存溢出的方法,感興趣的小伙伴可以了解下
    2025-01-01
  • GO語(yǔ)言打包成.exe程序的方法

    GO語(yǔ)言打包成.exe程序的方法

    Go語(yǔ)言以其高效的編譯能力和簡(jiǎn)潔的語(yǔ)法,能夠輕松打包生成Windows系統(tǒng)下的.exe可執(zhí)行文件,用戶只需安裝Go編譯器、編寫Go源代碼并使用gobuild命令指定輸出文件名即可完成編譯,感興趣的可以了解一下
    2024-10-10
  • golang中map增刪改查的示例代碼

    golang中map增刪改查的示例代碼

    在Go語(yǔ)言中,map是一種內(nèi)置的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)鍵值對(duì),本文主要介紹了golang中map增刪改查的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • 一文詳解Go語(yǔ)言切片是如何擴(kuò)容的

    一文詳解Go語(yǔ)言切片是如何擴(kuò)容的

    切片是一個(gè)擁有相同類型元素的可變長(zhǎng)度的序列,它是基于數(shù)組類型做的一層封裝。它非常靈活,支持自動(dòng)擴(kuò)容。所以本文就來(lái)看看Go語(yǔ)言切片是如何擴(kuò)容的吧
    2023-04-04
  • Go語(yǔ)言中的訪問(wèn)權(quán)限控制

    Go語(yǔ)言中的訪問(wèn)權(quán)限控制

    在?go?中進(jìn)行權(quán)限管理時(shí),推薦使用?grouper、casbin?和?sentry?框架,grouper?適合基于角色的訪問(wèn)控制,casbin?提供高級(jí)?rbac?功能,而?sentry?提供云托管權(quán)限服務(wù)和豐富的功能集,包括多因素認(rèn)證和活動(dòng)審核,這些框架有助于在電子商務(wù)網(wǎng)站等實(shí)際場(chǎng)景中實(shí)施細(xì)粒度的訪問(wèn)控制
    2024-08-08
  • Go語(yǔ)言利用泛型封裝常見(jiàn)的Map操作

    Go語(yǔ)言利用泛型封裝常見(jiàn)的Map操作

    Go?語(yǔ)言在?1.18?版本中引入了泛型,這是?Go?語(yǔ)言發(fā)展的一個(gè)重要里程碑,它極大地增強(qiáng)了語(yǔ)言的表達(dá)能力和靈活性,本文將通過(guò)泛型實(shí)現(xiàn)封裝常見(jiàn)的Map操作,感興趣的可以了解下
    2025-02-02

最新評(píng)論

台东县| 奈曼旗| 洛川县| 红桥区| 宁明县| 合江县| 筠连县| 长顺县| 汤原县| 清徐县| 塘沽区| 泗阳县| 兴和县| 永泰县| 时尚| 榕江县| 莲花县| 腾冲县| 贺兰县| 武城县| 育儿| 长子县| 大悟县| 延安市| 镇雄县| 陆川县| 班戈县| 甘德县| 怀柔区| 神池县| 宾川县| 宝兴县| 西和县| 西乡县| 永兴县| 镇坪县| 青岛市| 怀宁县| 台东县| 资溪县| 新乡市|