{"id":985,"date":"2025-01-16T00:35:09","date_gmt":"2025-01-15T16:35:09","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=985"},"modified":"2025-01-16T00:35:09","modified_gmt":"2025-01-15T16:35:09","slug":"go-huma-%e7%89%88%e6%9c%ac%e6%8e%a7%e5%88%b6%ef%bc%9a%e7%ae%80%e5%8c%96-api-%e7%ae%a1%e7%90%86%e5%b9%b6%e4%bc%98%e5%8c%96%e5%85%bc%e5%ae%b9%e6%80%a7%e4%b8%8e%e6%89%a9%e5%b1%95%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/go-huma-%e7%89%88%e6%9c%ac%e6%8e%a7%e5%88%b6%ef%bc%9a%e7%ae%80%e5%8c%96-api-%e7%ae%a1%e7%90%86%e5%b9%b6%e4%bc%98%e5%8c%96%e5%85%bc%e5%ae%b9%e6%80%a7%e4%b8%8e%e6%89%a9%e5%b1%95%e6%80%a7\/","title":{"rendered":"Go Huma \u7248\u672c\u63a7\u5236\uff1a\u7b80\u5316 API \u7ba1\u7406\u5e76\u4f18\u5316\u517c\u5bb9\u6027\u4e0e\u6269\u5c55\u6027"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c<strong>Huma<\/strong> \u662f\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u6846\u67b6\uff0c\u65e8\u5728\u7b80\u5316\u548c\u589e\u5f3a\u6784\u5efa RESTful API \u7684\u4f53\u9a8c\u3002\u5b83\u57fa\u4e8e Go \u7684\u6807\u51c6\u5e93 <code>net\/http<\/code>\uff0c\u5e76\u5728\u5176\u57fa\u7840\u4e0a\u5f15\u5165\u4e86\u7b80\u5316\u8def\u7531\u5b9a\u4e49\u3001\u9a8c\u8bc1\u3001\u6587\u6863\u751f\u6210\u7b49\u529f\u80fd\uff0c\u4f7f\u5f97 API \u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7248\u672c\u63a7\u5236\u662f\u5f00\u53d1 API \u65f6\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728 API \u53d8\u5316\u65f6\u4fdd\u6301\u517c\u5bb9\u6027\uff0c\u5e76\u786e\u4fdd\u4e0d\u540c\u7248\u672c\u7684\u5ba2\u6237\u7aef\u80fd\u591f\u987a\u5229\u5730\u4e0e\u670d\u52a1\u7aef\u4ea4\u4e92\u3002\u5728 Go Huma \u4e2d\uff0c\u7248\u672c\u63a7\u5236\u901a\u5e38\u662f\u5728\u8def\u7531\u5c42\u9762\u4e0a\u8fdb\u884c\u7ba1\u7406\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Go Huma \u7248\u672c\u63a7\u5236\u57fa\u7840<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Huma \u4e2d\uff0c\u7248\u672c\u63a7\u5236\u901a\u5e38\u901a\u8fc7 API \u8def\u7531\u7684\u8def\u5f84\u3001\u8def\u7531\u7ec4\u6216\u81ea\u5b9a\u4e49\u7684\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u3002\u7248\u672c\u63a7\u5236\u7684\u76ee\u6807\u662f\u80fd\u591f\u5728 API \u7248\u672c\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u786e\u4fdd\u73b0\u6709\u5ba2\u6237\u7aef\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u540c\u65f6\u5141\u8bb8\u65b0\u7248\u672c\u7684 API \u63d0\u4f9b\u65b0\u7684\u529f\u80fd\u6216\u6539\u53d8\u5df2\u6709\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5e38\u89c1\u7684\u7248\u672c\u63a7\u5236\u7b56\u7565<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u51e0\u79cd\u5e38\u89c1\u7684 API \u7248\u672c\u63a7\u5236\u7b56\u7565\uff0cGo Huma \u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u7b56\u7565\u6765\u7ec4\u7ec7 API \u8def\u7531\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u8def\u5f84\u7248\u672c\u63a7\u5236<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8def\u5f84\u7248\u672c\u63a7\u5236\u662f\u6700\u5e38\u89c1\u7684\u7248\u672c\u63a7\u5236\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u5728 API \u8def\u5f84\u4e2d\u6307\u5b9a\u7248\u672c\u53f7\u6765\u533a\u5206\u4e0d\u540c\u7684 API \u7248\u672c\u3002\u4f8b\u5982\uff0c<code>\/v1\/users<\/code> \u8868\u793a\u7248\u672c 1 \u7684\u7528\u6237 API\uff0c<code>\/v2\/users<\/code> \u8868\u793a\u7248\u672c 2 \u7684\u7528\u6237 API\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Huma \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8def\u7531\u7ec4\u6765\u5b9e\u73b0\u8fd9\u4e00\u7b56\u7565\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"github.com\/dizzyfool\/huma\"\n    \"net\/http\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Huma API \u5b9e\u4f8b\n    api := huma.NewAPI(\"My API\", \"1.0\")\n\n    \/\/ \u8def\u7531\u7248\u672c\u63a7\u5236: v1\n    api.Route(\"\/v1\/users\", func(r huma.Router) {\n        r.Get(\"\/profile\").HandleFunc(func(w http.ResponseWriter, r *http.Request) {\n            fmt.Fprintln(w, \"v1 Profile\")\n        })\n    })\n\n    \/\/ \u8def\u7531\u7248\u672c\u63a7\u5236: v2\n    api.Route(\"\/v2\/users\", func(r huma.Router) {\n        r.Get(\"\/profile\").HandleFunc(func(w http.ResponseWriter, r *http.Request) {\n            fmt.Fprintln(w, \"v2 Profile with new fields\")\n        })\n    })\n\n    http.ListenAndServe(\":8080\", api)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 <code>api.Route()<\/code> \u4e3a\u6bcf\u4e2a\u7248\u672c\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u8def\u7531\u7ec4\u3002\u5728\u7248\u672c <code>v1<\/code> \u4e2d\uff0c<code>\/v1\/users\/profile<\/code> \u8def\u5f84\u6307\u5411\u65e7\u7684 API \u5b9e\u73b0\uff0c\u800c <code>v2<\/code> \u5219\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u5b57\u6bb5\u6216\u884c\u4e3a\uff0c\u6307\u5411\u65b0\u7684 API \u5b9e\u73b0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u5b50\u8def\u7531\u7248\u672c\u63a7\u5236<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u901a\u8fc7\u5b50\u8def\u7531\u63a7\u5236\u7248\u672c\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7248\u672c\u63a7\u5236\u5728\u4e00\u4e2a\u529f\u80fd\u6a21\u5757\u4e2d\u8fdb\u884c\uff0c\u800c\u4e0d\u662f\u5728\u6574\u4e2a API \u8def\u5f84\u4e2d\uff0c\u5b50\u8def\u7531\u7248\u672c\u63a7\u5236\u4f1a\u662f\u4e00\u4e2a\u7406\u60f3\u9009\u62e9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"github.com\/dizzyfool\/huma\"\n    \"net\/http\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Huma API \u5b9e\u4f8b\n    api := huma.NewAPI(\"My API\", \"1.0\")\n\n    \/\/ \u5b9a\u4e49 \/users \u8def\u7531\u7ec4\n    users := api.Route(\"\/users\", func(r huma.Router) {\n        \/\/ \u7248\u672c\u63a7\u5236: v1\n        r.Route(\"\/v1\", func(r huma.Router) {\n            r.Get(\"\/profile\").HandleFunc(func(w http.ResponseWriter, r *http.Request) {\n                fmt.Fprintln(w, \"v1 Profile\")\n            })\n        })\n        \/\/ \u7248\u672c\u63a7\u5236: v2\n        r.Route(\"\/v2\", func(r huma.Router) {\n            r.Get(\"\/profile\").HandleFunc(func(w http.ResponseWriter, r *http.Request) {\n                fmt.Fprintln(w, \"v2 Profile with new fields\")\n            })\n        })\n    })\n\n    \/\/ \u542f\u52a8 HTTP \u670d\u52a1\n    http.ListenAndServe(\":8080\", users)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86 <code>\/users\/v1<\/code> \u548c <code>\/users\/v2<\/code> \u8def\u7531\uff0c\u4e24\u4e2a\u7248\u672c\u7684 API \u8def\u5f84\u5728\u540c\u4e00\u7ec4\u4e0b\u3002\u901a\u8fc7\u5b50\u8def\u7531\u7248\u672c\u63a7\u5236\uff0c\u53ef\u4ee5\u5c06\u4e0d\u540c\u7248\u672c\u7684 API \u805a\u5408\u5230\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u6a21\u5757\u4e0b\uff0c\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u8bf7\u6c42\u5934\u7248\u672c\u63a7\u5236<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u67d0\u4e9b\u573a\u666f\uff0c\u6211\u4eec\u5e0c\u671b\u901a\u8fc7\u8bf7\u6c42\u5934\u800c\u4e0d\u662f URL \u6765\u533a\u5206\u4e0d\u540c\u7248\u672c\u7684 API\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bf7\u6c42\u7684 <code>Accept<\/code> \u6216 <code>X-API-Version<\/code> \u5934\u6765\u6307\u5b9a API \u7684\u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"github.com\/dizzyfool\/huma\"\n    \"net\/http\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u65b0\u7684 Huma API \u5b9e\u4f8b\n    api := huma.NewAPI(\"My API\", \"1.0\")\n\n    \/\/ \u7248\u672c\u63a7\u5236: \u6839\u636e\u8bf7\u6c42\u5934\u6765\u9009\u62e9 API \u7248\u672c\n    api.Get(\"\/users\/profile\", func(w http.ResponseWriter, r *http.Request) {\n        version := r.Header.Get(\"X-API-Version\")\n\n        switch version {\n        case \"v1\":\n            fmt.Fprintln(w, \"v1 Profile\")\n        case \"v2\":\n            fmt.Fprintln(w, \"v2 Profile with new fields\")\n        default:\n            http.Error(w, \"Unsupported API version\", http.StatusNotAcceptable)\n        }\n    })\n\n    http.ListenAndServe(\":8080\", api)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u68c0\u67e5\u8bf7\u6c42\u5934\u4e2d\u7684 <code>X-API-Version<\/code> \u6765\u9009\u62e9\u4e0d\u540c\u7684 API \u7248\u672c\u3002\u5ba2\u6237\u7aef\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u80fd\u591f\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u7248\u672c\u7684 API\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u7248\u672c\u63a7\u5236\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4fdd\u6301\u5411\u540e\u517c\u5bb9\u6027<\/strong>\uff1a\u6bcf\u6b21\u53d1\u5e03\u65b0\u7248\u672c\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u8001\u7248\u672c\u7684\u652f\u6301\uff0c\u4ee5\u907f\u514d\u73b0\u6709\u7528\u6237\u7684\u4e2d\u65ad\u3002<\/li>\n\n\n\n<li><strong>\u660e\u786e\u7248\u672c\u53d8\u66f4<\/strong>\uff1a\u5f53 API \u6539\u52a8\u8f83\u5927\u65f6\uff0c\u53ef\u4ee5\u589e\u52a0\u7248\u672c\u53f7\u3002\u6bcf\u4e2a\u7248\u672c\u5e94\u8be5\u6e05\u6670\u6807\u6ce8\uff0c\u5e76\u6709\u660e\u786e\u7684\u6587\u6863\u8bf4\u660e\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong>\u6587\u6863\u751f\u6210<\/strong>\uff1a\u5728\u4f7f\u7528 Huma \u5f00\u53d1 API \u65f6\uff0c\u5229\u7528\u6846\u67b6\u81ea\u52a8\u751f\u6210\u7684\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u5ba2\u6237\u7aef\u5f00\u53d1\u8005\u4e86\u89e3 API \u7684\u7248\u672c\u53ca\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong>\u9010\u6b65\u5f03\u7528\u65e7\u7248\u672c<\/strong>\uff1a\u5f53\u4f60\u51b3\u5b9a\u5f03\u7528\u67d0\u4e2a\u7248\u672c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 API \u6587\u6863\u901a\u77e5\u5ba2\u6237\u7aef\uff0c\u5e76\u5728\u672a\u6765\u7248\u672c\u4e2d\u5b8c\u5168\u79fb\u9664\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go Huma \u63d0\u4f9b\u4e86\u4e00\u4e9b\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u7248\u672c\u63a7\u5236\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u66f4\u597d\u5730\u7ba1\u7406 API \u7684\u7248\u672c\u3002\u901a\u8fc7\u8def\u5f84\u7248\u672c\u63a7\u5236\u3001\u5b50\u8def\u7531\u7248\u672c\u63a7\u5236\u548c\u8bf7\u6c42\u5934\u7248\u672c\u63a7\u5236\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u6765\u6784\u5efa\u517c\u5bb9\u6027\u5f3a\u3001\u6613\u4e8e\u7ef4\u62a4\u7684 API \u7cfb\u7edf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0cHuma \u662f\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u6846\u67b6\uff0c\u65e8\u5728\u7b80\u5316\u548c\u589e\u5f3a\u6784\u5efa RESTful API \u7684\u4f53\u9a8c\u3002\u5b83\u57fa\u4e8e Go [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-985","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=985"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/985\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}