{"id":208124,"date":"2025-07-08T15:00:48","date_gmt":"2025-07-08T07:00:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208124\/"},"modified":"2025-07-08T15:00:48","modified_gmt":"2025-07-08T07:00:48","slug":"gorm-%e4%b8%8d%e6%94%af%e6%8c%81%e7%9a%84%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208124\/","title":{"rendered":"gorm \u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u63a5\u53e3"},"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>gorm \u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u63a5\u53e3<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 10:54:31<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300agorm \u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u63a5\u53e3\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>gorm \u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u63a5\u53e3\u3002<\/p>\n<p>\u5982\u4f55\u5c06\u4e0d\u540c\u7684\u7ed3\u6784\u4fdd\u5b58\u5230 json (gorm) \u4e2d\u7684\u4e00\u4e2a\u5b57\u6bb5\uff1f\u9700\u8981\u7c7b\u578b\u68c0\u67e5\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f json \u4e2d\u7684\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u6709\u9519\u8bef\u7684\u793a\u4f8b\u4ee3\u7801\uff1a\u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\uff1amodels.fields<\/p>\n<pre>\/\/ table questions (GORM)\ntype Questions struct {\n    ID\n    Fields  `json:\"fields\"`\n\/\/  .....\n}\n\ntype Fields interface{\n\/\/  ...\n}\n\n\/\/ radio ----------\ntype Radio struct {\n    Text string\n    Img string\n    \/\/  ...\n}\n\nfunc (d Radio) Value() (driver.Value, error) {\n    return json.Marshal(d)\n}\n\nfunc (d *Radio) Scan(value interface{}) error {\n    b, ok := value.([]byte)\n    if !ok {\n        return errors.New(\"type assertion to []byte failed\")\n    }\n    return json.Unmarshal(b, &amp;d)\n}\n\n\/\/ Checkbox ----------\ntype Checkbox struct {\n    Text string\n    MaxCheckbox   uint16\n    \/\/  ...\n}\n\nfunc (d Checkbox) Value() (driver.Value, error) {\n    return json.Marshal(d)\n}\n\nfunc (d *Checkbox) Scan(value interface{}) error {\n    b, ok := value.([]byte)\n    if !ok {\n        return errors.New(\"type assertion to []byte failed\")\n    }\n    return json.Unmarshal(b, &amp;d)\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u80fd\u5df2\u7ecf\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f46\u60a8\u9700\u8981\u4f7f\u7528 \u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300agorm \u4e0d\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u63a5\u53e3\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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-208124","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208124","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=208124"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208124\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}