{"id":207169,"date":"2025-07-08T09:28:53","date_gmt":"2025-07-08T01:28:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207169\/"},"modified":"2025-07-08T09:28:53","modified_gmt":"2025-07-08T01:28:53","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5rest-api%e4%b8%ad%e7%9a%84%e6%88%aa%e6%ad%a2%e6%97%a5%e6%9c%9f%e6%98%af%e5%90%a6%e5%a4%a7%e4%ba%8e%e6%98%8e%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207169\/","title":{"rendered":"\u5982\u4f55\u68c0\u67e5rest api\u4e2d\u7684\u622a\u6b62\u65e5\u671f\u662f\u5426\u5927\u4e8e\u660e\u5929"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u68c0\u67e5rest api\u4e2d\u7684\u622a\u6b62\u65e5\u671f\u662f\u5426\u5927\u4e8e\u660e\u5929<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 15:30:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u68c0\u67e5rest api\u4e2d\u7684\u622a\u6b62\u65e5\u671f\u662f\u5426\u5927\u4e8e\u660e\u5929\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5df2\u7ecf\u7f16\u5199\u4e86\u4e00\u4e2a\u5f85\u529e\u4e8b\u9879\u5904\u7406\u7a0b\u5e8f api\uff0c\u5e76\u60f3\u6dfb\u52a0\u4e00\u4e2a\u6761\u4ef6\u6765\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684 duedate \u662f\u5426\u5c0f\u4e8e\u660e\u5929\u65e5\u671f\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u7f16\u5199\u5b83\u800c\u4e0d\u662f\uff1f\uff1f\uff1f\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff1f<\/p>\n<pre>type Todo struct {\n    gorm.Model\n\n    Title string `json:\"title,omitempty\"`\n    Description string `json:\"description,omitempty\"`\n    DueDate time.Time `json:\"duedate,omitempty\"`\n\n    UserId uint   `json:\"user_id\"` \/\/The user that this todo belongs to\n}\n\nfunc  ValidateTodo(todo *Todo) (map[string]interface{}, bool) {\n\n    if todo.Title == \"\" {\n        return u.Message(false, \"Todo title should be on the payload\"), false\n    }\n\n    if len(todo.Title)&gt;30 {\n        return u.Message(false, \"Todo title should be less than 30 characters\"), false\n    }\n\n    if todo.Description == \"\" {\n        return u.Message(false, \"Todo description should be on the payload\"), false\n    }\n\n    if todo.DueDate&lt;time.Now(){\n       ?????\n    }\n    return u.Message(true, \"success\"), true\n\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 \u200b\u200b \u548c \u7684\u7ec4\u5408\u6765\u6267\u884c\u6b64\u64cd\u4f5c<\/p>\n<pre>duedate := time.now()\ntomorrow := time.now().adddate(0, 0, 1)\n\nif tomorrow.before(duedate) {\n    fmt.println(\"tomorrow is before your due date\")\n}<\/pre>\n<p><strong>\u7f16\u8f91\uff1a<\/strong>\u6c83\u5c14\u514b\u7684\u8bc4\u8bba\u5b9e\u9645\u4e0a\u63d0\u51fa\u4e86\u4e00\u4e2a\u5f88\u597d\u7684\u89c2\u70b9\u3002\u4e3a\u4e86\u53ef\u8bfb\u6027\uff0c\u4f7f\u7528 \u53ef\u80fd\u66f4\u7b26\u5408\u903b\u8f91\u3002\u6240\u4ee5\u4f60\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1b<\/p>\n<pre>duedate := time.now()\ntomorrow := time.now().adddate(0, 0, 1)\n\nif duedate.after(tomorrow) {\n    fmt.println(\"due date is after tomorrow\")\n}<\/pre>\n<p><strong>\u7f16\u8f912\uff1a<\/strong>\u6839\u636e\u5e0c\u671b\u660e\u5929\u7684\u65f6\u95f4\u4e3a 00:00:00 \u7684\u613f\u671b\uff0c\u5c06\u65f6\u95f4\u66f4\u65b0\u4e3a\u96f6<\/p>\n<pre>tomorrow := time.Now().AddDate(0, 0, 1)\ntomorrowZeroTime, err := time.Parse(\"Mon Jan 2 2006\", tomorrow.Format(\"Mon Jan 2 2006\"))\nif err != nil {\n    \/\/ Handle Error\n}\n\ndueDate := time.Now()\nif dueDate.After(tomorrowZeroTime) {\n    fmt.Println(\"Due date is after tomorrow\")\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u68c0\u67e5rest api\u4e2d\u7684\u622a\u6b62\u65e5\u671f\u662f\u5426\u5927\u4e8e\u660e\u5929\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207169","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207169"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207169\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}