{"id":989,"date":"2025-01-16T00:37:50","date_gmt":"2025-01-15T16:37:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=989"},"modified":"2025-01-16T00:37:50","modified_gmt":"2025-01-15T16:37:50","slug":"%e6%b7%b1%e5%85%a5%e6%8e%a2%e8%ae%a8%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%88%86%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/%e6%b7%b1%e5%85%a5%e6%8e%a2%e8%ae%a8%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%88%86%e9%a1%b5\/","title":{"rendered":"\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728 Go \u4e2d\u5b9e\u73b0\u5206\u9875"},"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\u5bf9 API \u54cd\u5e94\u8fdb\u884c\u5206\u9875\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u5206\u9875\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff0c\u907f\u514d\u52a0\u8f7d\u8fc7\u591a\u7684\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728 Go \u4e2d\u5b9e\u73b0\u5206\u9875\uff0c\u5e76\u4e14\u7ed3\u5408\u5e38\u89c1\u7684\u5206\u9875\u7b56\u7565\u6765\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u5206\u9875\u65b9\u6848\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5206\u9875\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u9875\u7684\u57fa\u672c\u6982\u5ff5\u662f\u5c06\u4e00\u4e2a\u5927\u7684\u6570\u636e\u96c6\u5206\u5272\u6210\u591a\u4e2a\u8f83\u5c0f\u7684\u6570\u636e\u5757\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7\u8bf7\u6c42\u4e0d\u540c\u7684\u9875\u7801\u6765\u83b7\u53d6\u76f8\u5e94\u7684\u6570\u636e\u3002\u5206\u9875\u901a\u5e38\u4f9d\u8d56\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9875\u7801\uff08page\uff09<\/strong>\uff1a\u7528\u6237\u8bf7\u6c42\u7684\u6570\u636e\u9875\u6570\u3002<\/li>\n\n\n\n<li><strong>\u6bcf\u9875\u5927\u5c0f\uff08limit\uff09<\/strong>\uff1a\u6bcf\u9875\u5305\u542b\u7684\u6570\u636e\u6761\u76ee\u6570\u3002<\/li>\n\n\n\n<li><strong>\u603b\u6570\uff08total\uff09<\/strong>\uff1a\u6570\u636e\u96c6\u7684\u603b\u6761\u76ee\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u603b\u9875\u6570\u3002<\/li>\n\n\n\n<li><strong>\u5f53\u524d\u9875\uff08current page\uff09<\/strong>\uff1a\u5f53\u524d\u8bf7\u6c42\u7684\u9875\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u8bbe\u8ba1\u5206\u9875 API \u54cd\u5e94<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8bbe\u8ba1\u5206\u9875\u65f6\uff0c\u54cd\u5e94\u4f53\u901a\u5e38\u4f1a\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\uff08data\uff09<\/strong>\uff1a\u5f53\u524d\u9875\u7684\u6570\u636e\u5217\u8868\u3002<\/li>\n\n\n\n<li><strong>\u5206\u9875\u4fe1\u606f\uff08pagination\uff09<\/strong>\uff1a\u5f53\u524d\u9875\u3001\u6bcf\u9875\u5927\u5c0f\u3001\u603b\u6570\u636e\u6761\u6570\u3001\u603b\u9875\u6570\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u54cd\u5e94\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"data\": &#91;\n    {\"id\": 1, \"name\": \"Item 1\"},\n    {\"id\": 2, \"name\": \"Item 2\"},\n    {\"id\": 3, \"name\": \"Item 3\"}\n  ],\n  \"pagination\": {\n    \"current_page\": 1,\n    \"page_size\": 3,\n    \"total_items\": 50,\n    \"total_pages\": 17\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Go \u4e2d\u5b9e\u73b0\u5206\u9875\u7684\u6b65\u9aa4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a API \u9700\u8981\u5206\u9875\u8fd4\u56de\u67d0\u79cd\u8d44\u6e90\uff08\u4f8b\u5982\u7528\u6237\u5217\u8868\uff09\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\u5206\u9875\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 1: \u5b9a\u4e49\u5206\u9875\u53c2\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u901a\u8fc7\u67e5\u8be2\u53c2\u6570\u6765\u63a5\u6536\u5206\u9875\u7684\u53c2\u6570\uff0c\u6bd4\u5982 <code>page<\/code> \u548c <code>limit<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"strconv\"\n)\n\nfunc getPaginationParams(r *http.Request) (int, int) {\n    \/\/ \u83b7\u53d6\u67e5\u8be2\u53c2\u6570 \"page\" \u548c \"limit\"\n    pageStr := r.URL.Query().Get(\"page\")\n    limitStr := r.URL.Query().Get(\"limit\")\n\n    \/\/ \u8bbe\u7f6e\u9ed8\u8ba4\u503c\n    page := 1\n    limit := 10\n\n    \/\/ \u89e3\u6790\u9875\u7801\u548c\u6bcf\u9875\u6761\u76ee\u6570\n    if pageStr != \"\" {\n        if parsedPage, err := strconv.Atoi(pageStr); err == nil {\n            page = parsedPage\n        }\n    }\n    if limitStr != \"\" {\n        if parsedLimit, err := strconv.Atoi(limitStr); err == nil {\n            limit = parsedLimit\n        }\n    }\n\n    return page, limit\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 2: \u83b7\u53d6\u6570\u636e<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u4ece\u6570\u636e\u5e93\u6216\u4e00\u4e2a\u7b80\u5355\u7684\u5217\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 SQL \u67e5\u8be2\u3001ORM \u6216\u5176\u4ed6\u6570\u636e\u6e90\u6765\u83b7\u53d6\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func getUsers(page, limit int) (&#91;]User, int, error) {\n    \/\/ \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u5217\u8868\n    users := &#91;]User{\n        {\"John Doe\", 1}, {\"Jane Smith\", 2}, {\"Alice Johnson\", 3},\n        {\"Bob Brown\", 4}, {\"Charlie White\", 5}, {\"David Black\", 6},\n    }\n\n    \/\/ \u83b7\u53d6\u603b\u6761\u76ee\u6570\n    totalItems := len(users)\n\n    \/\/ \u8ba1\u7b97\u5206\u9875\u7684\u8d77\u59cb\u4f4d\u7f6e\n    start := (page - 1) * limit\n    end := start + limit\n\n    \/\/ \u9632\u6b62\u7d22\u5f15\u8d8a\u754c\n    if start &gt;= totalItems {\n        return nil, totalItems, nil\n    }\n    if end &gt; totalItems {\n        end = totalItems\n    }\n\n    \/\/ \u83b7\u53d6\u5f53\u524d\u9875\u7684\u6570\u636e\n    return users&#91;start:end], totalItems, nil\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 3: \u8fd4\u56de\u5206\u9875\u54cd\u5e94<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u6570\u636e\u548c\u5206\u9875\u4fe1\u606f\u4e00\u8d77\u8fd4\u56de\u3002\u8fd4\u56de\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u636e\u548c\u5206\u9875\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func getPaginatedUsers(w http.ResponseWriter, r *http.Request) {\n    page, limit := getPaginationParams(r)\n\n    \/\/ \u83b7\u53d6\u5206\u9875\u6570\u636e\n    users, totalItems, err := getUsers(page, limit)\n    if err != nil {\n        http.Error(w, \"Failed to retrieve users\", http.StatusInternalServerError)\n        return\n    }\n\n    \/\/ \u8ba1\u7b97\u603b\u9875\u6570\n    totalPages := (totalItems + limit - 1) \/ limit\n\n    \/\/ \u6784\u5efa\u5206\u9875\u54cd\u5e94\n    response := map&#91;string]interface{}{\n        \"data\": users,\n        \"pagination\": map&#91;string]interface{}{\n            \"current_page\": page,\n            \"page_size\":    limit,\n            \"total_items\":  totalItems,\n            \"total_pages\":  totalPages,\n        },\n    }\n\n    \/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934\n    w.Header().Set(\"Content-Type\", \"application\/json\")\n    w.WriteHeader(http.StatusOK)\n\n    \/\/ \u8fd4\u56de JSON \u54cd\u5e94\n    err = json.NewEncoder(w).Encode(response)\n    if err != nil {\n        http.Error(w, \"Failed to encode response\", http.StatusInternalServerError)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5b8c\u6574\u793a\u4f8b\uff1a\u5b9e\u73b0\u5206\u9875\u7684 API<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"net\/http\"\n    \"strconv\"\n)\n\n\/\/ User \u7ed3\u6784\u4f53\ntype User struct {\n    Name string `json:\"name\"`\n    ID   int    `json:\"id\"`\n}\n\n\/\/ \u83b7\u53d6\u5206\u9875\u53c2\u6570\nfunc getPaginationParams(r *http.Request) (int, int) {\n    pageStr := r.URL.Query().Get(\"page\")\n    limitStr := r.URL.Query().Get(\"limit\")\n\n    page := 1\n    limit := 10\n\n    if pageStr != \"\" {\n        if parsedPage, err := strconv.Atoi(pageStr); err == nil {\n            page = parsedPage\n        }\n    }\n    if limitStr != \"\" {\n        if parsedLimit, err := strconv.Atoi(limitStr); err == nil {\n            limit = parsedLimit\n        }\n    }\n\n    return page, limit\n}\n\n\/\/ \u83b7\u53d6\u7528\u6237\u6570\u636e\nfunc getUsers(page, limit int) (&#91;]User, int, error) {\n    users := &#91;]User{\n        {\"John Doe\", 1}, {\"Jane Smith\", 2}, {\"Alice Johnson\", 3},\n        {\"Bob Brown\", 4}, {\"Charlie White\", 5}, {\"David Black\", 6},\n    }\n\n    totalItems := len(users)\n    start := (page - 1) * limit\n    end := start + limit\n    if start &gt;= totalItems {\n        return nil, totalItems, nil\n    }\n    if end &gt; totalItems {\n        end = totalItems\n    }\n\n    return users&#91;start:end], totalItems, nil\n}\n\n\/\/ API \u5904\u7406\u51fd\u6570\nfunc getPaginatedUsers(w http.ResponseWriter, r *http.Request) {\n    page, limit := getPaginationParams(r)\n\n    users, totalItems, err := getUsers(page, limit)\n    if err != nil {\n        http.Error(w, \"Failed to retrieve users\", http.StatusInternalServerError)\n        return\n    }\n\n    totalPages := (totalItems + limit - 1) \/ limit\n\n    response := map&#91;string]interface{}{\n        \"data\": users,\n        \"pagination\": map&#91;string]interface{}{\n            \"current_page\": page,\n            \"page_size\":    limit,\n            \"total_items\":  totalItems,\n            \"total_pages\":  totalPages,\n        },\n    }\n\n    w.Header().Set(\"Content-Type\", \"application\/json\")\n    w.WriteHeader(http.StatusOK)\n    err = json.NewEncoder(w).Encode(response)\n    if err != nil {\n        http.Error(w, \"Failed to encode response\", http.StatusInternalServerError)\n    }\n}\n\nfunc main() {\n    http.HandleFunc(\"\/users\", getPaginatedUsers)\n    fmt.Println(\"Server started at :8080\")\n    http.ListenAndServe(\":8080\", nil)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5206\u9875 API \u7684\u5e38\u89c1\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u9875\u53c2\u6570\u901a\u5e38\u901a\u8fc7\u67e5\u8be2\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u53ef\u4ee5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5e38\u89c1\u7684\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>page<\/strong>: \u9875\u7801\uff0c\u901a\u5e38\u4ece 1 \u5f00\u59cb\u3002<\/li>\n\n\n\n<li><strong>limit<\/strong>: \u6bcf\u9875\u6570\u636e\u6761\u6570\uff0c\u9ed8\u8ba4\u4e3a 10\u3002<\/li>\n\n\n\n<li><strong>sort<\/strong>: \u6392\u5e8f\u5b57\u6bb5\uff08\u53ef\u9009\uff09\u3002<\/li>\n\n\n\n<li><strong>filter<\/strong>: \u6570\u636e\u8fc7\u6ee4\u6761\u4ef6\uff08\u53ef\u9009\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET \/users?page=1&amp;limit=10&amp;sort=name<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4f18\u5316\u5206\u9875<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u9875\u529f\u80fd\u7684\u4f18\u5316\u4e3b\u8981\u5728\u4e8e\u5982\u4f55\u9ad8\u6548\u5730\u67e5\u8be2\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u7d22\u5f15<\/strong>\uff1a\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u7684\u67e5\u8be2\u5b57\u6bb5\uff08\u5982\u5206\u9875\u5b57\u6bb5\uff09\u6709\u9002\u5f53\u7684\u7d22\u5f15\uff0c\u907f\u514d\u5168\u8868\u626b\u63cf\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u524d\u8ba1\u7b97\u603b\u6570<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u63d0\u524d\u8ba1\u7b97\u603b\u6570\u636e\u91cf\uff08\u4f8b\u5982\u7f13\u5b58\u603b\u6570\uff09\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u9650\u5236\u6bcf\u9875\u5927\u5c0f<\/strong>\uff1a\u907f\u514d\u5206\u9875\u8bf7\u6c42\u8fc7\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u9650\u5236\u6bcf\u9875\u7684\u6570\u636e\u6761\u6570\uff08\u5982\u6700\u5927\u9650\u5236\u4e3a 100\uff09\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\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\u5b9e\u73b0\u5206\u9875\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u4f46\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u7075\u6d3b\u5730\u4f7f\u7528\u67e5\u8be2\u53c2\u6570\u548c\u5206\u9875\u54cd\u5e94\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u63d0\u9ad8 API \u7684\u6027\u80fd\uff0c\u786e\u4fdd\u7528\u6237\u4f53\u9a8c\u7684\u6d41\u7545\u6027\u3002\u5206\u9875\u529f\u80fd\u4e0d\u4ec5\u9650\u4e8e\u9759\u6001\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u67e5\u8be2\u3001\u641c\u7d22\u7ed3\u679c\u7b49\u573a\u666f\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7535\u5546\u5e73\u53f0\u3001\u793e\u4ea4\u7f51\u7edc\u7b49\u5404\u79cd\u7cfb\u7edf\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\u5bf9 API \u54cd\u5e94\u8fdb\u884c\u5206\u9875\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u5206\u9875\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u4f18 [&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-989","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/989","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=989"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/989\/revisions"}],"predecessor-version":[{"id":990,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/989\/revisions\/990"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}