<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] > [參考](https://www.topgoer.com/%E5%85%B6%E4%BB%96/Swagger.html) ## 示例 安裝 ``` go get -u github.com/swaggo/swag/cmd/swag go get github.com/swaggo/gin-swagger go get github.com/swaggo/gin-swagger/swaggerFiles ``` 在main 中添加 <details> <summary>main.go</summary> ``` package main import ( "net/http" "github.com/gin-gonic/gin" _ "github.com/student/0509/docs" ginSwagger "github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger/swaggerFiles" ) // @title Swagger Example API // @version 1.0 // @description This is a sample server celler server. // @termsOfService https://www.topgoer.com // @contact.name www.topgoer.com // @contact.url https://www.topgoer.com // @contact.email me@razeen.me // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @host 127.0.0.1:8080 // @BasePath /api/v1 func main() { r := gin.Default() r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) v1 := r.Group("/api/v1") { v1.GET("/hello", HandleHello) // v1.POST("/login", HandleLogin) // v1Auth := r.Use(HandleAuth) // { // v1Auth.POST("/upload", HandleUpload) // v1Auth.GET("/list", HandleList) // } } r.Run(":8080") } ``` </details> <br/> 執行命令 ``` > swag init 2020/05/13 16:28:02 Generate swagger docs.... 2020/05/13 16:28:02 Generate general API Info, search dir:./ 2020/05/13 16:28:02 create docs.go at docs/docs.go 2020/05/13 16:28:02 create swagger.json at docs/swagger.json 2020/05/13 16:28:02 create swagger.yaml at docs/swagger.yaml ``` 然后我們在`main.go`導入這個自動生成的`docs`包,運行 ``` package main import ( "github.com/gin-gonic/gin" ginSwagger "github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger/swaggerFiles" _ "github.com/razeencheng/demo-go/swaggo-gin/docs" ) // @title Swagger Example API // @version 1.0 // ... ``` 運行 ``` go run main.go ``` 瀏覽器打開http://127.0.0.1:8080/swagger/index.html ![](https://img.kancloud.cn/52/19/52193e35f9869b9b8444659c159465a1_1976x1008.png) 在Handle函數上添加注釋 <details> <summary>handle.go</summary> ``` // @Summary 測試SayHello // @Description 向你說Hello // @Tags 測試 // @Accept json // @Param who query string true "人名" // @Success 200 {string} string "{"msg": "hello Razeen"}" // @Failure 400 {string} string "{"msg": "who are you"}" // @Router /hello [get] func HandleHello(c *gin.Context) { who := c.Query("who") if who == "" { c.JSON(http.StatusBadRequest, gin.H{"msg": "who are u?"}) return } c.JSON(http.StatusOK, gin.H{"msg": "hello " + who}) } ``` </details> <br/> ## 優化 在生產環境中,不把文檔打包進去 在main.go聲明swagHandler,并在該參數不為空時才加入路由: ``` package main //... var swagHandler gin.HandlerFunc func main(){ // ... if swagHandler != nil { r.GET("/swagger/*any", swagHandler) } //... } ``` 我們將該參數在另外加了`build tag`的包中初始化 ``` // +build doc package main import ( _ "github.com/razeencheng/demo-go/swaggo-gin/docs" ginSwagger "github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger/swaggerFiles" ) func init() { swagHandler = ginSwagger.WrapHandler(swaggerFiles.Handler) } ``` `go build -tags "doc"`來打包帶文檔的包,直接`go build`來打包不帶文檔的包
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看