iT邦幫忙

0

關於golang swaggo

各位好,我是一個golang的新手

我最近在使用一個叫做swaggo的套件來寫我golang程式的api文件

我碰到了一個問題,用來產生文件的註解只有在main.go裡面有效用

如果我將那些註解移到我的api.go裡面,在swaggerUi上就會顯示no operation defined in spec,反之放在main.go裡面就會正確的顯示出來(註解指的是api operation annotation )

感覺好像是swaggo沒有掃到我在api.go裡面的註解?

還請各位幫我解惑,謝謝

powerc iT邦新手 1 級 ‧ 2021-02-26 17:13:36 檢舉
api.go有確實import套件?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-02-26 11:05:23

不妨檢查看看 package name

這篇的原因是 package 名稱不符

Oscorpio iT邦新手 5 級 ‧ 2021-02-26 14:06:13 檢舉

很感謝你的回答,不過好像跟package沒有關係

我之前有看了他們的github的 issue,問題好像是當我下swag init這個指令的時候,它只會執行main.go裡面的註解,而不會執行到我寫在api 裡的註解

不過目前我還沒找到怎麼解決這個問題的方法,我有試過官方github的一些解法,不過好像沒有幫助orz

不管怎麼樣,還是非常感謝你的回答

底下這段(swag init 之後那段)是否有可能?

Getting started

    Add comments to your API source code, See Declarative Comments Format.

    Download swag by using:

$ go get -u github.com/swaggo/swag/cmd/swag

To build from source you need Go (1.9 or newer).

Or download a pre-compiled binary from the release page.

    Run swag init in the project's root folder which contains the main.go file. This will parse your comments and generate the required files (docs folder and docs/docs.go).

$ swag init

Make sure to import the generated docs/docs.go so that your specific configuration gets init'ed. If your General API annotations do not live in main.go, you can let swag know with -g flag.

swag init -g http/api.go

資料來源

我要發表回答

立即登入回答