go gin web server Web服务器框架

彭阳朔
2023-12-01

一、源码及其使用步骤:

step 1,  想看源码的话。下载代码源码: GitHub - gin-gonic/gin: Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

step 2, 安装gin运行时的依赖库: go get -u github.com/gin-gonic/gin

安装之后的源码位置: C:\Users\vily\go\pkg\mod\github.com\gin-gonic\gin@v1.8.1\

step3, 下载相关依赖库,如下所示:

注意,先要在使用库的目录里执行 go mod init main 命令,这里的module名字假定为main

否则会报 'go get' is no longer supported outside a module 的错误。

go get -v github.com/ramya-rao-a/go-outline

go get -v github.com/sqs/goreturns

go get -v github.com/rogpeppe/godef

如果下载失败, 设置代理即可: go env -w GOPROXY=https://goproxy.cn

step 4, 可以写自己的代码测试了。

二、源码中一些知识点:

1. gin.H

位于 utils.go 文件内, 定义为:

// H is a shortcut for map[string]interface{}
type H map[string]any

2. go 语言中的HTTP 状态码 源码

        a. http package - net/http - Go Packages

        b.源码位置: C:/Program Files/Go/src/net/http/status.go

const (
	StatusContinue           = 100 // RFC 7231, 6.2.1
	StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
	StatusProcessing         = 102 // RFC 2518, 10.1
	StatusEarlyHints         = 103 // RFC 8297

	StatusOK                   = 200 // RFC 7231, 6.3.1
	StatusCreated              = 201 // RFC 7231, 6.3.2
	StatusAccepted             = 202 // RFC 7231, 6.3.3
	StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
	StatusNoContent            = 204 // RFC 7231, 6.3.5
	StatusResetContent         = 205 // RFC 7231, 6.3.6
	StatusPartialContent       = 206 // RFC 7233, 4.1
	StatusMultiStatus          = 207 // RFC 4918, 11.1
	StatusAlreadyReported      = 208 // RFC 5842, 7.1
	StatusIMUsed               = 226 // RFC 3229, 10.4.1
    ...
    ...
    ...
}

 类似资料: