到这里, 开发环境已经基本配置完成 .
0x02 创建第一个
Go
版本的
api
项目
Go
语言版本的
RestApi
框架有很多, 这里我们以
https://github.com/gin-gonic/gin
作为学习对象.
手动下载
gin
依赖项
打开命令行窗口, 执行命令:
go get -u github.com/gin-gonic/gin
执行正常结束后, 会在 %GOPATH%\src\github.com\gin-gonic\gin
目录下多出一大票代码.
如你所想, 这个命令, 就是把 github
上的代码下载到本地了.
one more thing
Go
编译成功后, 只有一个执行文件(便于发布?).
因此, 就不难理解 , 为什么下载依赖, 其实就是把依赖的源码给拉下来了.
作为一个不爱写文档, 又恨别人不写文档的的程序员, 我们当然选择Swagger
来做为接口说明输出啦.
这个神器可是Spring
项目的标配了, go
开发的接口当然也不能放松要求.
我们使用https://github.com/swaggo/gin-swagger作为 swagger
接入方案.
跟上面的流程一样, 我们先下载依赖
go get -u github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
go get -u github.com/alecthomas/template
在项目根目录(main.go
所在目录 ) , 执行命令行: swag init
,会在项目目录下生成一堆文件.
DOC
主体注释写法参考
https://swaggo.github.io/swaggo.io/declarative_comments_format/general_api_info.html
API
上面的注释写法请参考
https://swaggo.github.io/swaggo.io/declarative_comments_format/api_operation.html
package main
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
_ "hello_gin/docs"
// @title 国服最坑开发
// @version 1.0.1
// @description 神奇的API.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
r := gin.Default()
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.GET("/ping", pong)
r.Run() // listen and serve on 0.0.0.0:8080
// Ping Pong
// @Summary Ping Pong
// @Description get string by ID
// @Tags 健康检测
// @Produce json
// @Success 200
// @Router /ping [get]
func pong(ctx * gin.Context) {
ctx.JSON(200, gin.H{
"message": "pong",
每次修改注释后, 都要执行 swag init
, 然后再启动服务进行验证
启动服务后,打开网页 http://localhost:8080/swagger/index.html, 查看效果:
请尽量让自己的回复能够对别人有帮助
支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
图片支持拖拽、截图粘贴等方式上传
到这里, 开发环境已经基本配置完成 .
0x02 创建第一个Go
版本的api
项目
Go
语言版本的RestApi
框架有很多, 这里我们以https://github.com/gin-gonic/gin 作为学习对象.
手动下载gin
依赖项
打开命令行窗口, 执行命令:
go get -u github.com/gin-gonic/gin
执行正常结束后, 会在 %GOPATH%\src\github.com\gin-gonic\gin
目录下多出一大票代码.
如你所想, 这个命令, 就是把 github
上的代码下载到本地了.
one more thing
Go
编译成功后, 只有一个执行文件(便于发布?).
因此, 就不难理解 , 为什么下载依赖, 其实就是把依赖的源码给拉下来了.
作为一个不爱写文档, 又恨别人不写文档的的程序员, 我们当然选择Swagger
来做为接口说明输出啦.
这个神器可是Spring
项目的标配了, go
开发的接口当然也不能放松要求.
我们使用https://github.com/swaggo/gin-swagger作为 swagger
接入方案.
跟上面的流程一样, 我们先下载依赖
go get -u github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
go get -u github.com/alecthomas/template
在项目根目录(main.go
所在目录 ) , 执行命令行: swag init
,会在项目目录下生成一堆文件.
DOC
主体注释写法参考
https://swaggo.github.io/swaggo.io/declarative_comments_format/general_api_info.html
API
上面的注释写法请参考
https://swaggo.github.io/swaggo.io/declarative_comments_format/api_operation.html
package main
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
_ "hello_gin/docs"
// @title 国服最坑开发
// @version 1.0.1
// @description 神奇的API.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
r := gin.Default()
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.GET("/ping", pong)
r.Run() // listen and serve on 0.0.0.0:8080
// Ping Pong
// @Summary Ping Pong
// @Description get string by ID
// @Tags 健康检测
// @Produce json
// @Success 200
// @Router /ping [get]
func pong(ctx * gin.Context) {
ctx.JSON(200, gin.H{
"message": "pong",
每次修改注释后, 都要执行 swag init
, 然后再启动服务进行验证
启动服务后,打开网页 http://localhost:8080/swagger/index.html, 查看效果:
最高记录 5390
©2013-2024 studygolang.com Go语言中文网,中国 Golang 社区,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。
Powered by StudyGolang(Golang + MySQL) • · CDN 采用 七牛云
VERSION: V4.0.0 · 6.96997ms · 为了更好的体验,本站推荐使用 Chrome 或 Firefox 浏览器
京ICP备14030343号-1