{"id":127102,"date":"2020-05-31T13:07:05","date_gmt":"2020-05-31T05:07:05","guid":{"rendered":"http:\/\/4563.org\/?p=127102"},"modified":"2020-05-31T13:07:05","modified_gmt":"2020-05-31T05:07:05","slug":"%e9%97%ae%e8%80%81%e5%93%a5%e4%bb%ac%e4%b8%80%e4%b8%aa-go-gorm-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=127102","title":{"rendered":"\u95ee\u8001\u54e5\u4eec\u4e00\u4e2a go gorm \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u95ee\u8001\u54e5\u4eec\u4e00\u4e2a go gorm \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ifconfig <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>type Countries struct {  ID    uint32 `json:\"id\"`  Name  string `json:\"name\"`  Image string `json:\"image\" sql:\"-\"` }  type CountriesAll struct {  Countries  IsComment uint8 `json:\"is_common\"` } <\/code><\/pre>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0cA \u63a5\u53e3\u53ef\u80fd\u9700\u8981 Countries \u7684\u6570\u636e\uff0cB \u63a5\u53e3\u53ef\u80fd\u9700\u8981 CountriesAll \u7684\u6570\u636e\uff0cC \u63a5\u53e3\u9700\u8981 CountriesSimple \u7684\u6570\u636e\uff0c\u8fd9\u6837\u5c82\u4e0d\u662f\u8981\u5b9a\u4e49\u86ee\u591a\u4e2a\u7ed3\u6784\u4f53\u7684\uff1f<\/p>\n<p>\u73b0\u5728\u7684\u70e6\u607c\u662f\uff0c\u7528\u6237\u8868\u6709\u5341\u51e0\u4e2a\u5b57\u6bb5\uff0c\u53ef\u80fd\u4e00\u4e9b\u63a5\u53e3\u53ea\u9700\u8981\u7cbe\u7b80\u7684\u5b57\u6bb5\uff0c\u4e00\u4e9b\u63a5\u53e3\u9700\u8981\u5b8c\u6574\u7684\u5b57\u6bb5\uff0c\u7ed9\u540e\u53f0\u7684\u63a5\u53e3\u53c8\u662f\u53e6\u5916\u4e00\u4e9b\u5b57\u6bb5\uff0c\u6240\u4ee5\u4e3a\u4e86<strong>\u4fdd\u6301\u63a5\u53e3\u7684\u7cbe\u7b80\u548c\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684\u5b57\u6bb5\u8f93\u51fa<\/strong>\uff0c\u5728\u4e00\u4e2a model \u5185\u5b9a\u4e49\u591a\u4e2a\u7ed3\u6784\u4f53\uff0c\u8fd9\u6837\u5408\u7406\u4e48\uff1f\u8fd8\u662f\u5927\u5bb6\u662f\u600e\u4e48\u505a\u7684\uff1f<\/p>\n<p>PS:\u4e0d\u8fc7\u636e\u6211\u6240\u77e5\u54c8\uff0c\u8eab\u8fb9\u7684\u5199 go \u7684\u670b\u53cb\u662f\u7528 sql \u539f\u751f\u5199\u6cd5\uff0c\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u4e0d\u8fc7\u6211\u6bd4\u8f83\u559c\u6b22\u6a21\u578b\u7684\u5199\u6cd5\u5199\u8d77\u6765\u6bd4\u8f83\u7cbe\u7b80\u4e5f\u597d\u7ef4\u62a4<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2060653\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lvsshuttao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u5e73\u65f6\u90fd\u662f\u8fd9\u6837\u5199\u7684<br \/>&#8220;`<br \/>type Country struct { \/\/ \u7528\u4e8e\u6570\u636e\u5e93\u7684\u7ed3\u6784\u4f53\uff0c\u6ca1\u6709\u591a\u4f59\u7684\u5b57\u6bb5<br \/> Id int64 `json:&#8221;id&#8221;`<br \/> CreatedAt time.Time `json:&#8221;created_at&#8221;`<br \/> UpdatedAt time.Time `json:&#8221;updated_at&#8221;`<br \/> Name string `json:&#8221;name&#8221;`<br \/> Image string `json:&#8221;image&#8221;`<br \/>}<\/p>\n<p>type ParamCountry struct { \/\/ \u7528\u4e8e\u521b\u5efa\u8bb0\u5f55\u7684\u7ed3\u6784\u4f53<br \/> Id int64 `json:&#8221;id&#8221;`<br \/> Name string `json:&#8221;name&#8221;`<br \/> Image string `json:&#8221;image&#8221;`<br \/>}<\/p>\n<p>func (p *ParamCountry) ToModel() model.Country {<br \/> return model.Country{ <br \/> Id: p.Id,<br \/> \/\/ &#8230;<br \/> }<br \/>}<\/p>\n<p>type RespCountry struct { \/\/ \u7528\u4e8e\u8fd4\u56de\u6570\u636e\u7684\u7ed3\u6784\u4f53<br \/> Id int64 `json:&#8221;id&#8221;`<br \/> Name string `json:&#8221;name&#8221;`<br \/> Image string `json:&#8221;image&#8221;`<br \/>}<\/p>\n<p>func (p *RespCountry) FromModel(m *model.Country) {<br \/> p.Id = m.Id<br \/> \/\/ &#8230;<br \/>}<\/p>\n<p>type RespCountryDetail struct { \/\/ \u6269\u5c55\u4e86 RespCountry<br \/> RespCountry<br \/> \/\/ \u66f4\u591a\u7684\u5b57\u6bb5<br \/>}<br \/>&#8220;`<\/p>\n<p>\u5206\u5f00\u5199\u6709\u5229\u4e8e\u6269\u5c55\uff1b\u6bd4\u5982\u67e5\u8be2\u7ed3\u679c\u9700\u8981\u5176\u5b83\u8868\u5b57\u6bb5\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u5f80 `RespCountry` \u4e2d\u6dfb\u52a0\u5176\u5b83\u8868\u7684\u5b57\u6bb5\uff1b\u5982\u679c\u67e5\u8be2\u53c2\u6570\u9700\u8981\u652f\u6301\u6309\u5173\u952e\u5b57 `keyword`(\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u8868) \u67e5\u8be2\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5f80 `ParamCountry` \u6dfb\u52a0                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060654\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : airplayxcom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9001\u4f60\u4e00\u628a\u68ad\u5b50 https:\/\/github.com\/airplayx\/gormat                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060655\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ifconfig <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @airplayxcom \u6211\u7528\u5728\u7ebf\u7248\u7684 https:\/\/dev.ataotaoa.com\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060656\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zjj19950716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/colobu.com\/2020\/03\/19\/Custom-JSON-Marshalling-in-Go\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060657\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zjj19950716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zjj19950716 https:\/\/zhuanlan.zhihu.com\/p\/27472716                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060658\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ifconfig <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zjj19950716 \u5b66\u4e60\u5230\u4e86\uff0c\u611f\u8c22\u5206\u4eab                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060659\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yrj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u5de7\uff0c\u6b63\u597d\u8fd9\u51e0\u5929\u4e5f\u5728\u7528 gorm\uff0c\u4e5f\u9047\u5230\u4e86\u4e3b\u540c\u6837\u7684\u95ee\u9898\u3002\u6301\u7eed\u5173\u6ce8\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2060660\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hanggi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e00\u4e2a model \u5e94\u8be5\u5bf9\u5e94\u4e00\u4e2a\u8868\uff0c\u5982\u679c\u4f60\u6709\u591a\u79cd\u6570\u636e\u683c\u5f0f\u8fd4\u56de\u7684\u9700\u6c42\uff0c\u5e94\u8be5\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684 struct \u4e13\u95e8\u7528\u4e8e\u8fd4\u56de\u3002<br \/>\u53ef\u4ee5\u501f\u9274 #5 \u7ed9\u7684\u94fe\u63a5\u91cc\u5f97\u5404\u79cd\u65b9\u6cd5\uff0c\u628a\u4e0d\u540c\u7684\u8fd4\u56de\u7ed3\u6784\u8fdb\u884c\u62c6\u5206\u3001\u5408\u5e76\u3001\u5d4c\u5957\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u8001\u54e5\u4eec\u4e00\u4e2a go gorm \u7684\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127102"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127102"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127102\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}