{"id":57662,"date":"2024-10-10T20:06:14","date_gmt":"2024-10-10T12:06:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57662\/"},"modified":"2024-10-10T20:06:14","modified_gmt":"2024-10-10T12:06:14","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%b5%90%e6%a7%8b%e9%ab%94%e6%a8%99%e7%b1%a4%ef%bc%88struct-tag%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57662\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u7d50\u69cb\u9ad4\u6a19\u7c64\uff08Struct Tag\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%b5%90%e6%a7%8b%e9%ab%94%e6%a8%99%e7%b1%a4%ef%bc%88struct-tag%ef%bc%89-nRqeYSpXNi\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u7d50\u69cb\u9ad4\u6a19\u7c64\uff08Struct Tag\uff09<\/h1>\n<p>\u5728Go\u8a9e\u8a00\u4e2d\uff0c\u7d50\u69cb\u9ad4\uff08Struct\uff09\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u6578\u64da\u985e\u578b\uff0c\u7528\u65bc\u7d44\u7e54\u548c\u5c01\u88dd\u6578\u64da\u3002\u7d50\u69cb\u9ad4\u6a19\u7c64\uff08Struct Tag\uff09\u5247\u662f\u7d50\u69cb\u9ad4\u5b57\u6bb5\u7684\u4e00\u7a2e\u9644\u52a0\u4fe1\u606f\uff0c\u901a\u5e38\u7528\u65bc\u63cf\u8ff0\u5b57\u6bb5\u7684\u5143\u6578\u64da\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0eGo\u8a9e\u8a00\u4e2d\u7684\u7d50\u69cb\u9ad4\u6a19\u7c64\uff0c\u5305\u62ec\u5176\u7528\u9014\u3001\u8a9e\u6cd5\u4ee5\u53ca\u5be6\u969b\u61c9\u7528\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%b5%90%e6%a7%8b%e9%ab%94%e6%a8%99%e7%b1%a4%ef%bc%9f-nRqeYSpXNi\">\u4ec0\u9ebc\u662f\u7d50\u69cb\u9ad4\u6a19\u7c64\uff1f<\/h2>\n<p>\u7d50\u69cb\u9ad4\u6a19\u7c64\u662fGo\u8a9e\u8a00\u4e2d\u7d50\u69cb\u9ad4\u5b57\u6bb5\u7684\u9644\u52a0\u4fe1\u606f\uff0c\u901a\u5e38\u7528\u65bc\u6307\u5b9a\u5b57\u6bb5\u7684\u884c\u70ba\u6216\u5c6c\u6027\u3002\u9019\u4e9b\u6a19\u7c64\u53ef\u4ee5\u7528\u65bc\u5e8f\u5217\u5316\u3001\u53cd\u5e8f\u5217\u5316\u3001\u9a57\u8b49\u7b49\u64cd\u4f5c\u3002\u7d50\u69cb\u9ad4\u6a19\u7c64\u7684\u8a9e\u6cd5\u662f\u5c07\u6a19\u7c64\u653e\u5728\u5b57\u6bb5\u7684\u5f8c\u9762\uff0c\u4e26\u7528\u53cd\u5f15\u865f\uff08`\uff09\u5305\u570d\u3002<\/p>\n<h2 id=\"%e7%b5%90%e6%a7%8b%e9%ab%94%e6%a8%99%e7%b1%a4%e7%9a%84%e8%aa%9e%e6%b3%95-nRqeYSpXNi\">\u7d50\u69cb\u9ad4\u6a19\u7c64\u7684\u8a9e\u6cd5<\/h2>\n<p>\u7d50\u69cb\u9ad4\u6a19\u7c64\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>type Person struct {\n    Name string `json:\"name\" xml:\"name\"`\n    Age  int    `json:\"age\" xml:\"age\"`\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`Person`\u7d50\u69cb\u9ad4\u6709\u5169\u500b\u5b57\u6bb5\uff1a`Name`\u548c`Age`\u3002\u6bcf\u500b\u5b57\u6bb5\u5f8c\u9762\u90fd\u6709\u4e00\u500b\u6a19\u7c64\uff0c\u9019\u4e9b\u6a19\u7c64\u6307\u5b9a\u4e86\u5728JSON\u548cXML\u5e8f\u5217\u5316\u6642\u4f7f\u7528\u7684\u5b57\u6bb5\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e7%b5%90%e6%a7%8b%e9%ab%94%e6%a8%99%e7%b1%a4%e7%9a%84%e7%94%a8%e9%80%94-nRqeYSpXNi\">\u7d50\u69cb\u9ad4\u6a19\u7c64\u7684\u7528\u9014<\/h2>\n<ul>\n<li><strong>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff1a<\/strong>\u7d50\u69cb\u9ad4\u6a19\u7c64\u5e38\u7528\u65bc\u6307\u5b9a\u5728\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u904e\u7a0b\u4e2d\u4f7f\u7528\u7684\u5b57\u6bb5\u540d\u7a31\u3002\u4f8b\u5982\uff0c\u4f7f\u7528`json`\u6a19\u7c64\u53ef\u4ee5\u63a7\u5236JSON\u8f38\u51fa\u6642\u7684\u5b57\u6bb5\u540d\u7a31\u3002<\/li>\n<li><strong>\u6578\u64da\u9a57\u8b49\uff1a<\/strong>\u7d50\u69cb\u9ad4\u6a19\u7c64\u4e5f\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u9a57\u8b49\uff0c\u901a\u904e\u7b2c\u4e09\u65b9\u5eab\uff08\u5982`validator`\uff09\u4f86\u6aa2\u67e5\u5b57\u6bb5\u7684\u6709\u6548\u6027\u3002<\/li>\n<li><strong>ORM\u6620\u5c04\uff1a<\/strong>\u5728\u4f7f\u7528ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6642\uff0c\u7d50\u69cb\u9ad4\u6a19\u7c64\u53ef\u4ee5\u7528\u4f86\u6307\u5b9a\u6578\u64da\u5eab\u4e2d\u7684\u5217\u540d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b-nRqeYSpXNi\">\u5be6\u969b\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u7d50\u69cb\u9ad4\u6a19\u7c64\u7684\u5be6\u969b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Go\u7684`encoding\/json`\u5305\u9032\u884cJSON\u5e8f\u5217\u5316\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc main() {\n    p := Person{Name: \"Alice\", Age: 30}\n    jsonData, err := json.Marshal(p)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    fmt.Println(string(jsonData)) \/\/ \u8f38\u51fa: {\"name\":\"Alice\",\"age\":30}\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b`Person`\u7d50\u69cb\u9ad4\uff0c\u4e26\u4f7f\u7528`json`\u6a19\u7c64\u4f86\u6307\u5b9aJSON\u8f38\u51fa\u6642\u7684\u5b57\u6bb5\u540d\u7a31\u3002\u7576\u6211\u5011\u5c07`Person`\u5be6\u4f8b\u5e8f\u5217\u5316\u70baJSON\u6642\uff0c\u8f38\u51fa\u7684\u5b57\u6bb5\u540d\u7a31\u5c07\u7b26\u5408\u6a19\u7c64\u7684\u5b9a\u7fa9\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-nRqeYSpXNi\">\u7d50\u8ad6<\/h2>\n<p>\u7d50\u69cb\u9ad4\u6a19\u7c64\u5728Go\u8a9e\u8a00\u4e2d\u662f\u4e00\u500b\u5f37\u5927\u7684\u7279\u6027\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6578\u64da\u3002\u7121\u8ad6\u662f\u5728\u5e8f\u5217\u5316\u3001\u6578\u64da\u9a57\u8b49\u9084\u662fORM\u6620\u5c04\u4e2d\uff0c\u7d50\u69cb\u9ad4\u6a19\u7c64\u90fd\u80fd\u63d0\u4f9b\u984d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u60a8\u7684\u9805\u76ee\u4e2d\u4f7f\u7528Go\u8a9e\u8a00\u7684\u7d50\u69cb\u9ad4\u6a19\u7c64\u6709\u9032\u4e00\u6b65\u7684\u8208\u8da3\uff0c\u6216\u662f\u9700\u8981\u9ad8\u6548\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u5de5\u4f5c\uff0c\u8acb\u96a8\u6642\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Go\u8a9e\u8a00\u4e2d\u7684\u7d50\u69cb\u9ad4\u6a19\u7c64\uff08Struct Tag\uff09\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528\u6a19\u7c64\u4f86\u589e\u5f37\u7d50\u69cb\u9ad4\u7684\u529f\u80fd\u8207\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57662","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57662","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=57662"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57662\/revisions"}],"predecessor-version":[{"id":57663,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57662\/revisions\/57663"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}