添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
英勇无比的仙人掌  ·  AttributeError: Api ...·  2 天前    · 
健身的显示器  ·  什么是 Swagger?- ...·  1 周前    · 
兴奋的开水瓶  ·  springdoc-openapi v1.8.0·  3 周前    · 
害羞的大象  ·  ASP.NETCoreWebAPI-在Cor ...·  4 月前    · 
英勇无比的双杠  ·  DateFormat | ...·  4 月前    · 
无聊的长颈鹿  ·  “IntrinsicAttributes& ...·  1 周前    · 
害羞的咖啡豆  ·  Developer Community·  1 月前    · 
踢足球的茶壶  ·  Access ...·  1 月前    · 

到这里, 开发环境已经基本配置完成 .

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