{"id":508,"date":"2024-05-26T23:53:02","date_gmt":"2024-05-26T15:53:02","guid":{"rendered":"http:\/\/moyisuiying.com\/?p=508"},"modified":"2024-05-27T00:00:43","modified_gmt":"2024-05-26T16:00:43","slug":"go-swagger%e4%bd%bf%e7%94%a8%e7%ae%80%e4%bb%8b%e4%bb%a5%e5%8f%8ahertz%e6%a1%86%e6%9e%b6%e4%b8%ad%e9%9b%86%e6%88%90go-swagger","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/gostudy\/508.html","title":{"rendered":"go swagger\u4f7f\u7528\u7b80\u4ecb\u4ee5\u53cahertz\u6846\u67b6\u4e2d\u96c6\u6210go-swagger"},"content":{"rendered":"\n<h1>1.\u80cc\u666f<\/h1>\n\n\n\n<p>\u6700\u8fd1\u8d1f\u8d23\u7684\u9879\u76ee\u6a21\u5757\u9700\u8981\u4ece\u4e00\u4e2a\u5927\u5355\u4f53\u4ee3\u7801\u4ed3\u5e93\u4e2d\u8fc1\u79fb\u5230\u65b0\u7684\u4ee3\u7801\u4ed3\u5e93\u53e6\u8d77\u4e00\u4e2a\u65b0\u670d\u52a1\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u6211\u8d1f\u8d23\u7684\u4ee3\u7801\u6a21\u5757\u4ece\u8001\u4ee3\u7801\u4ed3\u5e93\u4e2d\u8fc1\u79fb\u5230\u65b0\u7684\u4ee3\u7801\u4ed3\u5e93\u4e2d\u7136\u540e\u8fdb\u884c\u91cd\u6784\u3002\u8001\u7684\u5355\u4f53\u670d\u52a1\u662f\u4e00\u4e2a\u57fa\u4e8ego\u7684web\u6846\u67b6gin\u8fdb\u884c\u5305\u88c5\u540e\u7684web\u6846\u67b6\uff0c\u800c\u6211\u8d1f\u8d23\u7684\u4ee3\u7801\u8fc1\u79fb\u5230\u65b0\u7684\u670d\u52a1\u540e\u4f7f\u7528\u516c\u53f8\u5185\u90e8\u7684go web\u6846\u67b6hertz\u8fdb\u884c\u91cd\u6784\u3002\u8001\u7684\u4ee3\u7801\u6846\u67b6\u4e2d\u6ca1\u6709\u63a5\u5165\u7c7b\u4f3cswagger\u76f8\u5173\u7684\u751f\u6210open API\u6587\u6863\u7684\u7ec4\u4ef6\uff0c\u5bfc\u81f4\u5728\u524d\u540e\u7aef\u8054\u8c03\u8fc7\u7a0b\u4e2d\u5bf9\u4e8ehttp API\u63a5\u53e3\u7684\u5b9a\u4e49\u53ea\u80fd\u662f\u5199\u5728\u98de\u4e66\u6587\u6863\u4e2d\uff0c\u6c9f\u901a\u8d77\u6765\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002\u56e0\u6b64\u4e3a\u4e86\u63d0\u9ad8\u524d\u540e\u7aefhttp\u63a5\u53e3\u6587\u6863\u7684\u5bf9\u63a5\u6548\u7387\uff0c\u5728\u65b0\u7684\u8fc1\u79fb\u670d\u52a1\u4e2d\u63a5\u5165go swagger\u751f\u6210http API\u63a5\u53e3\u7684\u6587\u6863\u3002<\/p>\n\n\n\n<h1>2.go swagger\u7684\u5b89\u88c5\u548c\u4f7f\u7528<\/h1>\n\n\n\n<h2>2.1\u5b89\u88c5swagger cli<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"> #  go\u7684\u7248\u672c\u662f go1.20.3 linux\/amd64<br> go install github.com\/swaggo\/swag\/cmd\/swag@latest<br> <br> # \u67e5\u770bswagger\u7684\u7248\u672c<br> swag -v<br>\/\/ swag version v1.16.2<\/pre>\n\n\n\n<h2>2.2swagger cli\u7684\u4f7f\u7528<\/h2>\n\n\n\n<h3>2.2.1\u67e5\u770b swag cli\u652f\u6301\u7684\u547d\u4ee4<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">swag init -h<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u5f53\u524dswag\u53ea\u652f\u6301\u4e09\u4e2a\u547d\u4ee4\uff1a<\/p>\n\n\n\n<ol><li>swag init<\/li><li>swag fmt<\/li><li>swag help<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">NAME:<br> &nbsp; swag - Automatically generate RESTful API documentation with Swagger 2.0 for Go.<br>\u200b<br>USAGE:<br> &nbsp; swag [global options] command [command options] [arguments...]<br>\u200b<br>VERSION:<br> &nbsp; v1.16.2<br>\u200b<br>COMMANDS:<br> &nbsp; init, i  Create docs.go<br> &nbsp; fmt, f &nbsp; format swag comments<br> &nbsp; help, h  Shows a list of commands or help for one command<br>\u200b<br>GLOBAL OPTIONS:<br> &nbsp; --help, -h &nbsp; &nbsp; show help (default: false)<br> &nbsp; --version, -v  print the version (default: false)<\/pre>\n\n\n\n<h3>2.2.2swag init\u7684\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">swag init -h<br>NAME:<br> &nbsp; swag init - Create docs.go<br>\u200b<br>USAGE:<br> &nbsp; swag init [command options] [arguments...]<br>\u200b<br>OPTIONS:<br> &nbsp; --generalInfo value, -g value &nbsp; &nbsp; &nbsp; &nbsp;  API\u901a\u7528\u4fe1\u606f\u6240\u5728\u7684go\u6e90\u6587\u4ef6\u8def\u5f84\uff0c\u5982\u679c\u662f\u76f8\u5bf9\u8def\u5f84\u5219\u57fa\u4e8eAPI\u89e3\u6790\u76ee\u5f55 (\u9ed8\u8ba4: \"main.go\")<br> &nbsp; --dir value, -d value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  API\u89e3\u6790\u76ee\u5f55 (\u9ed8\u8ba4: \".\/\")<br> &nbsp; --exclude value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u89e3\u6790\u626b\u63cf\u65f6\u6392\u9664\u7684\u76ee\u5f55\uff0c\u591a\u4e2a\u76ee\u5f55\u53ef\u7528\u9017\u53f7\u5206\u9694\uff08\u9ed8\u8ba4\uff1a\u7a7a\uff09<br> &nbsp; --propertyStrategy value, -p value &nbsp; &nbsp; \u7ed3\u6784\u4f53\u5b57\u6bb5\u547d\u540d\u89c4\u5219\uff0c\u4e09\u79cd\uff1asnakecase,camelcase,pascalcase (\u9ed8\u8ba4: \"camelcase\")<br> &nbsp; --output value, -o value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u6587\u4ef6(swagger.json, swagger.yaml and doc.go)\u8f93\u51fa\u76ee\u5f55 (\u9ed8\u8ba4: \".\/docs\")<br> &nbsp; --parseVendor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u662f\u5426\u89e3\u6790vendor\u76ee\u5f55\u91cc\u7684go\u6e90\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4e0d<br> &nbsp; --parseDependency &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u662f\u5426\u89e3\u6790\u4f9d\u8d56\u76ee\u5f55\u4e2d\u7684go\u6e90\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4e0d<br> &nbsp; --markdownFiles value, --md value &nbsp; &nbsp;  \u6307\u5b9aAPI\u7684\u63cf\u8ff0\u4fe1\u606f\u6240\u4f7f\u7528\u7684markdown\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55<br> &nbsp; --generatedTime &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u662f\u5426\u8f93\u51fa\u65f6\u95f4\u5230\u8f93\u51fa\u6587\u4ef6docs.go\u7684\u9876\u90e8\uff0c\u9ed8\u8ba4\u662f<br> &nbsp; --codeExampleFiles value, --cef value  \u89e3\u6790\u5305\u542b\u7528\u4e8e x-codeSamples \u6269\u5c55\u7684\u4ee3\u7801\u793a\u4f8b\u6587\u4ef6\u7684\u6587\u4ef6\u5939\uff0c\u9ed8\u8ba4\u7981\u7528<br> &nbsp; --parseInternal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u89e3\u6790 internal \u5305\u4e2d\u7684go\u6587\u4ef6\uff0c\u9ed8\u8ba4\u7981\u7528<br> &nbsp; --parseDepth value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u4f9d\u8d56\u89e3\u6790\u6df1\u5ea6 (\u9ed8\u8ba4: 100)<br> &nbsp; --instanceName value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8bbe\u7f6e\u6587\u6863\u5b9e\u4f8b\u540d (\u9ed8\u8ba4: \"swagger\")<\/pre>\n\n\n\n<p>\u5728swag init\u547d\u4ee4\u4e2d\uff0c\u4ee5\u6211\u7684\u7ecf\u9a8c\u6765\u770b\u4e3b\u8981\u9700\u8981\u5173\u6ce8\u4ee5\u4e0b\u51e0\u4e2a\u547d\u4ee4\u53c2\u6570\uff1a<\/p>\n\n\n\n<ol><li>--dir value, -d value API\u89e3\u6790\u76ee\u5f55 (\u9ed8\u8ba4: \".\/\")\uff0c\u591a\u4e2a\u76ee\u5f55\u53ef\u4ee5\u7528\u82f1\u6587\u9017\u53f7\",\"\u9694\u5f00\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8be5\u53c2\u6570\uff0c\u5c06\u4f1a\u9ed8\u8ba4\u89e3\u6790\u5f53\u524d\u76ee\u5f55 .\/ \u4e0b\u7684\u6240\u6709API\u6ce8\u91ca\u3002\u8be5\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u76ee\u5f55\uff0c\u6bcf\u4e2a\u76ee\u5f55\u4e4b\u95f4\u7528\u3002\u6bd4\u5982\u6307\u5b9a\u751f\u6210\u4e24\u4e2a\u6587\u4ef6\u5939 .\/test1\u3001.\/test2\u4e0b\u7684\u6240\u6709API\u6587\u6863\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\u6307\u5b9a\uff1aswag init -d .\/test1,.\/test2\u3002<\/li><li>--generalInfo value, -g value API\u901a\u7528\u4fe1\u606f\u6240\u5728\u7684go\u6e90\u6587\u4ef6\u8def\u5f84.\u5982\u679c\u4e0d\u6307\u5b9a\u8be5\u53c2\u6570\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528\u5f53\u524d\u9879\u76ee\u76ee\u5f55\u4e0b .\/main.go\u4f5c\u4e3aAPI\u901a\u7528\u4fe1\u606f\u6240\u5728\u7684go\u6e90\u6587\u4ef6\u8def\u5f84\u3002\u5982\u679c\u6307\u5b9a\u4e86 -d\u53c2\u6570\uff0c\u90a3\u4e48\u5219\u662f\u76f8\u5bf9\u4e8e-d\u53c2\u6570\u76ee\u5f55\u4e0b\u7684.go\u6587\u4ef6\u8def\u5f84\u3002\u6bd4\u5982<\/li><\/ol>\n\n\n\n<p>swag init -d .\/test1,.\/test2 -g swagger_api.go\uff0c\u90a3\u4e48\u5c31\u8bf4\u660eAPI\u901a\u7528\u4fe1\u606f\u6240\u5728\u7684go\u6e90\u6587\u4ef6 swagger_api.go\u662f\u5728 .\/test1\u6216\u8005 .\/test2\u76ee\u5f55\u4e4b\u4e0b\u3002<\/p>\n\n\n\n<ol start=\"3\"><li>--exclude value \u89e3\u6790\u626b\u63cf\u65f6\u6392\u9664\u7684\u76ee\u5f55\uff0c\u591a\u4e2a\u76ee\u5f55\u53ef\u7528\u9017\u53f7\u5206\u9694\uff08\u9ed8\u8ba4\uff1a\u7a7a\uff09\u3002\u6307\u5b9a\u4e86\u76ee\u5f55\u4e4b\u540e\u89e3\u6790API\u65f6\u5019\u4e0d\u4f1a\u89e3\u6790\u8be5\u76ee\u5f55\u5217\u8868\u4e0b\u7684API\u3002<\/li><li>--parseVendor \u662f\u5426\u89e3\u6790vendor\u76ee\u5f55\u91cc\u7684go\u6e90\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4e0d\u4e0d\u89e3\u6790\u3002<\/li><li>--parseDependency \u662f\u5426\u89e3\u6790\u4f9d\u8d56\u76ee\u5f55\u4e2d\u7684go\u6e90\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4e0d\u89e3\u6790\u3002\u5982\u679c\u9700\u8981\u89e3\u6790\u7684API\u4e2d\u5f15\u7528\u4e86\u4f9d\u8d56\u6587\u4ef6\u7684\u7ed3\u6784\u4f53\uff0c\u90a3\u4e48\u9700\u8981\u6307\u5b9a\u8be5\u53c2\u6570\u3002<\/li><\/ol>\n\n\n\n<h3>2.2.3 swag fmt\u7684\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">swag fmt<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">NAME:<br> &nbsp; swag fmt - format swag comments \u7528\u4e8e\u683c\u5f0f\u5316API\u7684\u6ce8\u91ca<br>\u200b<br>USAGE:<br> &nbsp; swag fmt [command options] [arguments...]<br>\u200b<br>OPTIONS:<br> &nbsp; --dir value, -d value &nbsp; &nbsp; &nbsp; &nbsp;  API\u89e3\u6790\u76ee\u5f55 (\u9ed8\u8ba4: \".\/\")<br> &nbsp; --exclude value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  \u89e3\u6790\u626b\u63cf\u65f6\u6392\u9664\u7684\u76ee\u5f55\uff0c\u591a\u4e2a\u76ee\u5f55\u53ef\u7528\u9017\u53f7\u5206\u9694\uff08\u9ed8\u8ba4\uff1a\u7a7a\uff09<br> &nbsp; --generalInfo value, -g value  API\u901a\u7528\u4fe1\u606f\u6240\u5728\u7684go\u6e90\u6587\u4ef6\u8def\u5f84\uff0c\u5982\u679c\u662f\u76f8\u5bf9\u8def\u5f84\u5219\u57fa\u4e8eAPI\u89e3\u6790\u76ee\u5f55 (\u9ed8\u8ba4: \"main.go\")<br> &nbsp; --help, -h &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; show help (default: false)<\/pre>\n\n\n\n<h1>3.hertz\u6846\u67b6\u4e2d\u5f15\u5165go-swagger<\/h1>\n\n\n\n<h2>3.1\u5feb\u901f\u751f\u6210hert\u670d\u52a1\u4ee3\u7801<\/h2>\n\n\n\n<p>\u5177\u4f53\u53c2\u8003\uff1a<a href=\"https:\/\/www.cloudwego.io\/zh\/docs\/hertz\/tutorials\/toolkit\/install\/\" target=\"_blank\"  rel=\"nofollow\" >hz \u5b89\u88c5<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5728 GOPATH \u5916\u6267\u884c\uff0c\u9700\u8981\u6307\u5b9a go mod \u540d<br>hz new -module hertz-web<br>\u200b<br># \u6574\u7406 &amp; \u62c9\u53d6\u4f9d\u8d56<br>go mod tidy<br>\u200b<\/pre>\n\n\n\n<p>\u5220\u9664\u4e0d\u5fc5\u8981\u7684\u5305\u76ee\u5f55\uff0c\u5e76\u65b0\u5efa swagger_demo,types\u76ee\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.<br>\u251c\u2500\u2500 build.sh<br>\u251c\u2500\u2500 go.mod<br>\u251c\u2500\u2500 go.sum<br>\u251c\u2500\u2500 main.go &nbsp;           # main\u65b9\u6cd5\u5165\u53e3<br>\u251c\u2500\u2500 router.go<br>\u251c\u2500\u2500 router_gen.go<br>\u251c\u2500\u2500 swagger_demo &nbsp; &nbsp; &nbsp; &nbsp;# \u5b58\u653eAPI\u63a5\u53e3\u4ee5\u53ca swagger\u901a\u7528\u4fe1\u606f\u63a5\u53e3\u7684\u5305<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 swagger_api.go &nbsp;# \u5305\u542b\u4e86swagger\u901a\u7528\u4fe1\u606f\u63a5\u53e3\u4ee5\u53ca\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u7684\u63a5\u53e3<br>\u2514\u2500\u2500 types &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5b58\u653e\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u7ed3\u6784\u4f53<br> &nbsp;  \u2514\u2500\u2500 view.go<br>\u200b<\/pre>\n\n\n\n<h2>3.2\u5f15\u5165go-swagger<\/h2>\n\n\n\n<h3>3.2.1 view.go\u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    @author: 24029<br>\u200b<br>@since: 2023\/9\/3 22:03:24<br>@desc:<br>*\/<br>package types<br>\u200b<br>\/\/ BaseRes \u54cd\u5e94\u7684\u57fa\u7c7b,\u8fd9\u91cc\u5305\u542b\u6cdb\u578bT<br>type BaseRes[T any] struct {<br>    Code int &nbsp; &nbsp;\/\/ 0\u6210\u529f\uff0c1\u5931\u8d25<br>    Msg &nbsp;string \/\/ \u54cd\u5e94\u4fe1\u606f<br>    Data T &nbsp; &nbsp; &nbsp;\/\/ \u54cd\u5e94\u6570\u636e<br>}<br>\u200b<br>type AddUserReq struct {<br>    Name &nbsp; &nbsp; &nbsp; &nbsp;string `json:\"name,omitempty\"  validate:\"true\"` &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7528\u6237\u540d,\u5fc5\u586b<br>    Password &nbsp; &nbsp;string `json:\"password,omitempty\" validate:\"true\"` &nbsp; &nbsp; \/\/ \u7528\u6237\u5bc6\u7801\uff0c\u5fc5\u586b<br>    Age &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp;`json:\"age,omitempty\" validate:\"false\"` &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u7528\u6237\u5e74\u9f84\uff0c\u9009\u586b<br>    Description string `json:\"description,omitempty\" validate:\"false\"` \/\/ \u7528\u6237\u63cf\u8ff0\uff0c\u9009\u586b<br>}<br>type GetUserReq struct {<br>    Name &nbsp; &nbsp; string `json:\"name,omitempty\"  validate:\"true\"` &nbsp; &nbsp;\/\/ \u7528\u6237\u540d,\u5fc5\u586b<br>    Password string `json:\"password,omitempty\" validate:\"true\"` \/\/ \u7528\u6237\u5bc6\u7801\uff0c\u5fc5\u586b<br>}<br>\u200b<br>type GetUserRes struct {<br>    ID &nbsp; &nbsp; &nbsp; int64 &nbsp;\/\/ \u7528\u6237ID<br>    Name &nbsp; &nbsp; string `json:\"name,omitempty\"` &nbsp; &nbsp; \/\/ \u7528\u6237\u540d<br>    Password string `json:\"password,omitempty\"` \/\/ \u7528\u6237\u5bc6\u7801<br>    Age &nbsp; &nbsp; &nbsp;int &nbsp; &nbsp;`json:\"age\"` &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7528\u6237\u5e74\u9f84<br>}<br>\u200b<br>type DeleteUserReq struct {<br>    UserID string `json:\"user_id,omitempty\"  validate:\"true\"`<br>}<br>\u200b<br>type UserDetailsReq struct {<br>    Name &nbsp; &nbsp;string `json:\"name,omitempty\"  validate:\"true\"` \/\/ \u7528\u6237\u540d\uff0c\u5fc5\u586b<br>    Age &nbsp; &nbsp; string `json:\"age,omitempty\"  validate:\"true\"` &nbsp;\/\/ \u7528\u6237\u5e74\u9f84,\u5fc5\u586b<br>    Details string `json:\"details\"  validate:\"false\" ` &nbsp; &nbsp; &nbsp;\/\/ \u7528\u6237\u8be6\u7ec6\uff0c\u9009\u586b<br>}<br>\u200b<\/pre>\n\n\n\n<h3>3.2.2swagger_api.go\u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    @author: 24029<br>\u200b<br>@since: 2023\/9\/3 21:47:51<br>@desc:<br>*\/<br>package swagger_demo<br>\u200b<br>import (<br>    \"context\"<br>    \"github.com\/cloudwego\/hertz\/pkg\/app\"<br>    \"hertz-web\/types\"<br>    \"net\/http\"<br>)<br>\u200b<br>\/\/ SaggerAPI godoc<br>\/\/<br>\/\/  @title                      \u6d4b\u8bd5 hertz\u96c6\u6210swagger API\u7ec4\u4ef6<br>\/\/  @version                    1.0<br>\/\/  @description                \u7528\u6237\u6d4b\u8bd5hertz\u6846\u67b6\u96c6\u6210go swagger\u7ec4\u4ef6.<br>\/\/  @termsOfService             http:\/\/swagger.io\/terms\/<br>\/\/  @contact.name               API Support<br>\/\/  @contact.url                http:\/\/www.swagger.io\/support<br>\/\/  @contact.email              support@swagger_demo.io<br>\/\/  @license.name               Apache 2.0<br>\/\/  @license.url                http:\/\/www.apache.org\/licenses\/LICENSE-2.0.html<br>\/\/  @host                       localhost:8888<br>\/\/  @BasePath                   \/api\/v1<br>\/\/  @securityDefinitions.basic  BasicAuth<br>\/\/  @externalDocs.description   OpenAPI<br>\/\/  @externalDocs.url           https:\/\/swagger.io\/resources\/open-api\/<br>func SaggerAPI() {<br>\u200b<br>}<br>\u200b<br>\/\/ GetUser godoc<br>\/\/<br>\/\/  @Summary        \u6839\u636e\u7528\u6237\u540d\u548c\u7528\u6237\u5bc6\u7801\u83b7\u53d6\u7528\u6237\u4fe1\u606f<br>\/\/  @Description    1.\u5982\u679c\u7528\u6237\u540d\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u63d0\u793a\u7528\u6237\u8be5\u7528\u6237\u4e0d\u5b58\u5728<br>\/\/  @Description    2.\u5982\u679c\u5bc6\u7801\u4e0d\u6b63\u5e38\u90a3\u4e48\u63d0\u793a\u7528\u6237\u5bc6\u7801\u4e0d\u6b63\u786e\u3002<br>\/\/  @Accept         application\/json<br>\/\/  @Produce        application\/json<br>\/\/  @Param          name        query       string  true    \"\u7528\u6237\u540d\"<br>\/\/  @Param          password    query       string  true    \"\u7528\u6237\u5bc6\u7801\"<br>\/\/  @Success        200         {object}    types.BaseRes[types.GetUserRes]<br>\/\/  @Failure        400         {object}    types.BaseRes[error]<br>\/\/  @Failure        401         {object}    types.BaseRes[error]<br>\/\/  @Failure        404         {object}    types.BaseRes[error]<br>\/\/  @Router         \/user\/get [get]<br>func GetUser(c context.Context, ctx *app.RequestContext) {<br>    var req types.GetUserReq<br>    err := ctx.BindAndValidate(&amp;req)<br>    if err != nil {<br>        ctx.JSON(http.StatusBadRequest, types.BaseRes[error]{<br>            Code: 0,<br>            Msg: &nbsp;\"\u53c2\u6570\u7ed1\u5b9a\u5931\u8d25\",<br>            Data: err,<br>        })<br>        return<br>    }<br>    res := types.GetUserRes{<br>        ID: &nbsp; &nbsp; &nbsp; 1,<br>        Name: &nbsp; &nbsp; \"\",<br>        Password: \"\",<br>        Age: &nbsp; &nbsp; &nbsp;0,<br>    }<br>    result := types.BaseRes[types.GetUserRes]{<br>        Code: 0,<br>        Data: res,<br>        Msg: &nbsp;\"\",<br>    }<br>    ctx.JSON(http.StatusOK, result)<br>}<br>\u200b<br>\/\/ AddUser godoc<br>\/\/<br>\/\/  @Summary        \u65b0\u589e\u7528\u6237\u4fe1\u606f<br>\/\/  @Description    1.\u5982\u679c\u7528\u6237\u540d\u4e0d\u7b26\u5408\u89c4\u8303\uff0c\u5219\u7981\u6b62\u65b0\u589e\u8be5\u7528\u6237<br>\/\/  @Description    2.\u5982\u679c\u7528\u6237\u540d\u5df2\u5b58\u5728\uff0c\u90a3\u4e48\u65b0\u589e\u7528\u6237\u5931\u8d25<br>\/\/  @Accept         application\/json<br>\/\/  @Produce        application\/json<br>\/\/  @Param          req body        types.AddUserReq    true    \"\u65b0\u589e\u7528\u6237\u8bf7\u6c42\u53c2\u6570\"<br>\/\/  @Success        200 {object}    types.BaseRes[any]<br>\/\/  @Failure        400 {object}    types.BaseRes[error]<br>\/\/  @Failure        401 {object}    types.BaseRes[error]<br>\/\/  @Failure        404 {object}    types.BaseRes[error]<br>\/\/  @Failure        500 {object}    types.BaseRes[error]<br>\/\/  @Router         \/user\/add [post]<br>func AddUser(c context.Context, ctx *app.RequestContext) {<br>    var req types.AddUserReq<br>    err := ctx.BindAndValidate(&amp;req)<br>    if err != nil {<br>        ctx.JSON(http.StatusBadRequest, types.BaseRes[error]{<br>            Code: 0,<br>            Msg: &nbsp;\"\u53c2\u6570\u7ed1\u5b9a\u5931\u8d25\",<br>            Data: err,<br>        })<br>        return<br>    }<br>    result := types.BaseRes[any]{<br>        Code: 0,<br>        Msg: &nbsp;\"\",<br>    }<br>    ctx.JSON(http.StatusOK, result)<br>}<br>\u200b<br>\/\/ DeleteUser godoc<br>\/\/<br>\/\/  @Summary        \u6839\u636e\u7528\u6237ID\u5220\u9664\u7528\u6237\u4fe1\u606f<br>\/\/  @Description    1.\u5982\u679c\u7528\u6237\u540d\u4e0d\u7b26\u5408\u89c4\u8303\uff0c\u5219\u5220\u9664\u5931\u8d25<br>\/\/  @Description    2.\u5982\u679c\u7528\u6237\u540d\u5df2\u5b58\u5728\uff0c\u90a3\u4e48\u53ef\u4ee5\u5220\u9664\u8be5\u7528\u6237<br>\/\/  @Accept         application\/json<br>\/\/  @Produce        application\/json<br>\/\/  @Param          req body        types.DeleteUserReq true    \"\u5220\u9664\u7528\u6237\u8bf7\u6c42\u53c2\u6570\"<br>\/\/  @Success        200 {object}    types.BaseRes[any]<br>\/\/  @Failure        400 {object}    types.BaseRes[error]<br>\/\/  @Failure        401 {object}    types.BaseRes[error]<br>\/\/  @Failure        404 {object}    types.BaseRes[error]<br>\/\/  @Failure        500 {object}    types.BaseRes[error]<br>\/\/  @Router         \/user\/delete [delete]<br>func DeleteUser(c context.Context, ctx *app.RequestContext) {<br>    var req types.DeleteUserReq<br>    err := ctx.BindAndValidate(&amp;req)<br>    if err != nil {<br>        ctx.JSON(http.StatusBadRequest, types.BaseRes[error]{<br>            Code: 0,<br>            Msg: &nbsp;\"\u53c2\u6570\u7ed1\u5b9a\u5931\u8d25\",<br>            Data: err,<br>        })<br>        return<br>    }<br>    result := types.BaseRes[any]{<br>        Code: 0,<br>        Msg: &nbsp;\"\",<br>    }<br>    ctx.JSON(http.StatusOK, result)<br>}<br>\u200b<br>\/\/ GetUserByID godoc<br>\/\/<br>\/\/  @Summary        \u6839\u636e\u7528\u6237ID\u83b7\u53d6\u7528\u6237\u4fe1\u606f<br>\/\/  @Description    \u5982\u679c\u7528\u6237\u540d\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u8fd4\u56de\u7a7a\u4fe1\u606f\uff1b\u5982\u679c\u7528\u6237\u5b58\u5728\u90a3\u4e48\u8fd4\u56de\u5177\u4f53\u4fe1\u606f<br>\/\/  @Accept         application\/json<br>\/\/  @Produce        application\/json<br>\/\/  @Param          user_id     query       string  true    \"\u7528\u6237ID\"<br>\/\/  @Param          password    query       string  true    \"\u7528\u6237\u5bc6\u7801\"<br>\/\/  @Success        200         {object}    types.UserDetailsReq<br>\/\/  @Failure        400         {object}    types.BaseRes[error]<br>\/\/  @Failure        401         {object}    types.BaseRes[error]<br>\/\/  @Failure        404         {object}    types.BaseRes[error]<br>\/\/  @Failure        500         {object}    types.BaseRes[error]<br>\/\/  @Router         \/user\/get\/:id [get]<br>func GetUserByID(c context.Context, ctx *app.RequestContext) {<br>    var req types.GetUserReq<br>    err := ctx.BindAndValidate(&amp;req)<br>    if err != nil {<br>        ctx.JSON(http.StatusBadRequest, types.BaseRes[error]{<br>            Code: 0,<br>            Msg: &nbsp;\"\u53c2\u6570\u7ed1\u5b9a\u5931\u8d25\",<br>            Data: err,<br>        })<br>        return<br>    }<br>    res := types.GetUserRes{<br>        ID: &nbsp; &nbsp; &nbsp; 1,<br>        Name: &nbsp; &nbsp; \"\",<br>        Password: \"\",<br>        Age: &nbsp; &nbsp; &nbsp;0,<br>    }<br>    result := types.BaseRes[types.GetUserRes]{<br>        Code: 0,<br>        Data: res,<br>        Msg: &nbsp;\"\",<br>    }<br>    ctx.JSON(http.StatusOK, result)<br>}<br>\u200b<\/pre>\n\n\n\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n<ol><li>\u5728API\u6ce8\u91ca\u4e2d\uff0c\u7528\u6237\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u5982\u679c\u6709\u5305\u542b\u6cdb\u578b\uff0c\u90a3\u4e48\u5728\u4f7f\u7528\u7c7b\u578b\u65f6\u5019\u9700\u8981\u5c06\u6cdb\u578b\u6307\u5b9a\u4e3a\u5177\u4f53\u7c7b\u578b\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/li><\/ol>\n\n\n\n<p>\u6bd4\u5982 types.BaseRes[T any]\u7684\u81ea\u5b9a\u4e49BaseRes\u662f\u4e00\u4e2a\u5305\u542b\u6cdb\u578b\u7684\u7ed3\u6784\u4f53\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u5019\u9700\u8981\u6307\u5b9a\u5176\u5177\u4f53\u7c7b\u578b\uff1a types.BaseRes[types.GetUserRes],\u8fd9\u6837\u624d\u80fd\u6b63\u5e38\u89e3\u6790\u6210\u529f\u3002<\/p>\n\n\n\n<h3>3.2.3\u751f\u6210 swag doc\u6587\u6863\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u683c\u5f0f\u5316\u5e76\u89e3\u6790 .\/swagger_demo,.\/types\u5305\u4e0b\u7684API\u6ce8\u91ca\u751f\u6210 swag API\u6587\u6863<br>swag fmt &amp; swag init -d .\/swagger_demo,.\/types -g swagger_api.go <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">[1] 20301<br>2023\/09\/03 23:23:03 Generate swagger docs....<br>2023\/09\/03 23:23:03 Generate general API Info, search dir:.\/swagger_demo<br>[1]  + 20301 done &nbsp; &nbsp; &nbsp; swag fmt<br>2023\/09\/03 23:23:06 Generate general API Info, search dir:.\/types<br>2023\/09\/03 23:23:07 Generating types.BaseRes-types_GetUserRes<br>2023\/09\/03 23:23:07 Generating types.GetUserRes<br>2023\/09\/03 23:23:07 Generating types.BaseRes-error<br>2023\/09\/03 23:23:07 Generating types.AddUserReq<br>2023\/09\/03 23:23:07 Generating types.BaseRes-any<br>2023\/09\/03 23:23:07 Generating types.DeleteUserReq<br>2023\/09\/03 23:23:07 Generating types.UserDetailsReq<br>2023\/09\/03 23:23:07 create docs.go at docs\/docs.go<br>2023\/09\/03 23:23:07 create swagger.json at docs\/swagger.json<br>2023\/09\/03 23:23:07 create swagger.yaml at docs\/swagger.yaml<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">.\/<br>\u251c\u2500\u2500 build.sh<br>\u251c\u2500\u2500 docs &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#\u751f\u6210\u7684swagger API\u6587\u6863\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 docs.go<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 swagger.json<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 swagger.yaml<br>\u251c\u2500\u2500 go.mod<br>\u251c\u2500\u2500 go.sum<br>\u251c\u2500\u2500 main.go<br>\u251c\u2500\u2500 router.go<br>\u251c\u2500\u2500 router_gen.go<br>\u251c\u2500\u2500 swagger_demo<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 swagger_api.go<br>\u2514\u2500\u2500 types<br> &nbsp;  \u2514\u2500\u2500 view.go<br>\u200b<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/raw.githubusercontent.com\/LJF2402901363\/typora-images\/main\/202309032341291.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20230903234145966\"\/><\/figure >\n<noscript><img src=\"https:\/\/raw.githubusercontent.com\/LJF2402901363\/typora-images\/main\/202309032341291.png\" alt=\"image-20230903234145966\"\/><\/figure><\/noscript>\n\n\n\n<p>\u4f7f\u7528goland\u67e5\u770b<\/p>\n\n\n\n<h3>3.2.4\u5728main.go\u6587\u4ef6\u4e2d\u5f15\u5165\u9002\u914dhertz\u6846\u67b6\u7684\u4e2d\u95f4\u4ef6 hertz-contrib\/swagger<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b89\u88c5 swaggo\/files<br>go  install github.com\/swaggo\/files<br># \u5b89\u88c5hertz\u6846\u67b6\u9002\u914dswagger\u7684\u4e2d\u95f4\u4ef6<br>go install github.com\/hertz-contrib\/swagger<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Code generated by hertz generator.<br>\u200b<br>package main<br>\u200b<br>import (<br>    \"github.com\/cloudwego\/hertz\/pkg\/app\/server\"<br>    \"github.com\/hertz-contrib\/swagger\"<br>    swaggerFiles \"github.com\/swaggo\/files\"<br>    _ \"hertz-web\/docs\" &nbsp; &nbsp; &nbsp; &nbsp;  \/\/ \u5fc5\u987b\u8981\u5f15\u5165\u751f\u6210swagger API\u4ee3\u7801\u7684docs\u5305\u8fdb\u884c\u521d\u59cb\u5316 init()\u65b9\u6cd5<br>    \"hertz-web\/swagger_demo\"<br>)<br>\u200b<br>\u200b<br>func main() {<br>    h := server.Default()<br>\u200b<br>    h.GET(\"\/user\/get\", swagger_demo.GetUser)<br>    h.POST(\"\/user\/add\", swagger_demo.AddUser)<br>    h.DELETE(\"\/user\/delete\", swagger_demo.DeleteUser)<br>    h.GET(\"\/user\/get\/:id\", swagger_demo.GetUserByID)<br>\u200b<br>    url := swagger.URL(\"http:\/\/localhost:8888\/swagger\/doc.json\") \/\/ The url pointing to API definition<br> &nbsp;  \/\/ swagger API\u9996\u9875<br> &nbsp;  h.GET(\"\/swagger\/*any\", swagger.WrapHandler(swaggerFiles.Handler, url))<br>\u200b<br>    h.Spin()<br>}<br>\u200b<\/pre>\n\n\n\n<h2>3.3\u542f\u52a8hertz\u670d\u52a1<\/h2>\n\n\n\n<p>\u7f16\u8bd1\u9879\u76ee\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">go build .\/<\/pre>\n\n\n\n<p>\u542f\u52a8\u9879\u76ee\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\/hert-web<\/pre>\n\n\n\n<p>\u8bbf\u95eeswagger API\u9996\u9875\uff1a <a href=\"http:\/\/localhost:8888\/swagger\/index.html\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/localhost:8888\/swagger\/index.html<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/raw.githubusercontent.com\/LJF2402901363\/typora-images\/main\/202309040015672.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20230904001515518\"\/><\/figure >\n<noscript><img src=\"https:\/\/raw.githubusercontent.com\/LJF2402901363\/typora-images\/main\/202309040015672.png\" alt=\"image-20230904001515518\"\/><\/figure><\/noscript>\n\n\n\n<h1>4.\u58f0\u660e\u5f0f\u6ce8\u91ca\u8be6\u89e3<\/h1>\n\n\n\n<p>\u4e2d\u6587\u7248\u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/swaggo\/swag\/blob\/master\/README_zh-CN.md\" target=\"_blank\"  rel=\"nofollow\" >swag\u4e2d\u6587\u4f7f\u7528\u6587\u6863<\/a><\/p>\n\n\n\n<p>\u82f1\u6587\u7248\u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/swaggo\/swag\/blob\/master\/README.md\" target=\"_blank\"  rel=\"nofollow\" >swag\u4e2d\u6587\u4f7f\u7528\u6587\u6863<\/a><\/p>\n\n\n\n<p><strong>\uff08\u5efa\u8bae\u76f4\u63a5\u770b\u82f1\u6587\u7248\u6587\u6863\uff0c\u56e0\u4e3a\u82f1\u6587\u7248\u6587\u6863\u4f1a\u5b9e\u65f6\u66f4\u65b0\u5230\u6700\u65b0\uff0c\u4f46\u662f\u4e2d\u6587\u7248\u4e0d\u4e00\u5b9a\u4f1a\u66f4\u65b0\u5230\u6700\u65b0\uff09\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u80cc\u666f \u6700\u8fd1\u8d1f\u8d23\u7684\u9879\u76ee\u6a21\u5757\u9700\u8981\u4ece\u4e00\u4e2a\u5927\u5355\u4f53\u4ee3\u7801\u4ed3\u5e93\u4e2d\u8fc1\u79fb\u5230\u65b0\u7684\u4ee3\u7801\u4ed3\u5e93\u53e6\u8d77\u4e00\u4e2a\u65b0\u670d\u52a1\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u6211\u8d1f\u8d23\u7684\u4ee3\u7801\u6a21\u5757\u4ece\u8001\u4ee3\u7801\u4ed3\u5e93\u4e2d\u8fc1 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[229],"tags":[228,227],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/508"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}