{"id":998,"date":"2025-01-16T00:44:38","date_gmt":"2025-01-15T16:44:38","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=998"},"modified":"2025-01-16T00:44:38","modified_gmt":"2025-01-15T16:44:38","slug":"go-%e5%ba%8f%e5%88%97%e5%8c%96%e6%b7%b1%e5%85%a5%e8%a7%a3%e6%9e%90%ef%bc%9a%e7%bb%93%e6%9e%84%e6%a0%87%e7%ad%be%e3%80%81%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e9%ab%98%e6%95%88%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/go-%e5%ba%8f%e5%88%97%e5%8c%96%e6%b7%b1%e5%85%a5%e8%a7%a3%e6%9e%90%ef%bc%9a%e7%bb%93%e6%9e%84%e6%a0%87%e7%ad%be%e3%80%81%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e9%ab%98%e6%95%88%e5%ae%9e\/","title":{"rendered":"Go \u5e8f\u5217\u5316\u6df1\u5165\u89e3\u6790\uff1a\u7ed3\u6784\u6807\u7b7e\u3001\u9519\u8bef\u5904\u7406\u4e0e\u9ad8\u6548\u5b9e\u6218\u6280\u5de7"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Go \u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff08\u6216\u79f0\u7f16\u7801\u548c\u89e3\u7801\uff09\u662f\u5904\u7406\u6570\u636e\u4ea4\u6362\u65f6\u7684\u5173\u952e\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06\u6570\u636e\u7ed3\u6784\u8f6c\u5316\u4e3a\u7279\u5b9a\u683c\u5f0f\uff08\u5982 JSON\u3001XML \u6216 protobuf \u7b49\uff09\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u5916\u90e8\u7cfb\u7edf\u3001\u6587\u4ef6\u6216\u7f51\u7edc\u8fdb\u884c\u4ea4\u4e92\u3002Go \u63d0\u4f9b\u4e86 <code>encoding\/json<\/code> \u548c <code>encoding\/xml<\/code> \u7b49\u6807\u51c6\u5e93\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 Go \u4e2d\u7684\u5e8f\u5217\u5316\u8981\u70b9\uff0c\u91cd\u70b9\u5173\u6ce8\u7ed3\u6784\u6807\u7b7e\u3001\u9519\u8bef\u5904\u7406\u4ee5\u53ca\u5b9e\u9645\u7528\u4f8b\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>\u7ed3\u6784\u6807\u7b7e\uff08Struct Tags\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u4e2d\u7684\u7ed3\u6784\u4f53\u6807\u7b7e\uff08struct tags\uff09\u7528\u4e8e\u6307\u5b9a\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65f6\u5b57\u6bb5\u7684\u884c\u4e3a\u3002\u6807\u7b7e\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u6307\u793a\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u5f0f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406 JSON \u6216\u5176\u4ed6\u683c\u5f0f\u65f6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>JSON \u5e8f\u5217\u5316\u4e2d\u7684\u7ed3\u6784\u6807\u7b7e<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 JSON \u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\uff0c\u7ed3\u6784\u4f53\u5b57\u6bb5\u4f1a\u6839\u636e\u6807\u7b7e\u6307\u5b9a\u7684\u540d\u79f0\u8fdb\u884c\u7f16\u7801\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u7ed3\u6784\u4f53\u6807\u7b7e\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    FirstName string `json:\"first_name\"`\n    LastName  string `json:\"last_name\"`\n    Age       int    `json:\"age,omitempty\"`\n}\n\nfunc main() {\n    p := Person{\n        FirstName: \"John\",\n        LastName:  \"Doe\",\n        Age:       30,\n    }\n\n    data, err := json.Marshal(p)\n    if err != nil {\n        fmt.Println(\"Error marshalling:\", err)\n        return\n    }\n\n    fmt.Println(string(data))  \/\/ \u8f93\u51fa\uff1a{\"first_name\":\"John\",\"last_name\":\"Doe\",\"age\":30}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>json:\"first_name\"<\/code> \u7ed3\u6784\u6807\u7b7e\u6307\u5b9a\u4e86\u5e8f\u5217\u5316\u65f6\u5b57\u6bb5\u540d\u7684\u6620\u5c04\uff0c<code>json:\"age,omitempty\"<\/code> \u6807\u7b7e\u8868\u793a\u5982\u679c <code>Age<\/code> \u4e3a\u96f6\u503c\uff08\u4f8b\u5982 0\uff09\uff0c\u8be5\u5b57\u6bb5\u5c06\u88ab\u7701\u7565\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>\u5e38\u89c1\u6807\u7b7e\u9009\u9879<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>omitempty<\/code>: \u4ec5\u5728\u5b57\u6bb5\u7684\u96f6\u503c\u60c5\u51b5\u4e0b\u7701\u7565\u5b57\u6bb5\uff08\u4f8b\u5982\u7a7a\u5b57\u7b26\u4e32\u3001\u96f6\u6570\u5b57\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>-<\/code>: \u5ffd\u7565\u8be5\u5b57\u6bb5\uff0c\u4e0d\u53c2\u4e0e\u5e8f\u5217\u5316\u3002<\/li>\n\n\n\n<li><code>string<\/code>: \u5c06\u5b57\u6bb5\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\uff08\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b\uff0c\u5e38\u7528\u4e8e JSON \u4e2d\u7684\u65e5\u671f\u7b49\u5b57\u6bb5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u9519\u8bef\u5904\u7406\uff08Error Handling\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u901a\u5e38\u4f1a\u6d89\u53ca\u5230\u9519\u8bef\u5904\u7406\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u4e0d\u7b26\u5408\u9884\u671f\u683c\u5f0f\u65f6\u3002\u9519\u8bef\u5904\u7406\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u4fdd\u8bc1\u4e86\u7a0b\u5e8f\u80fd\u591f\u5e94\u5bf9\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u65e0\u6548\u7684 JSON \u6216\u9519\u8bef\u7684\u5b57\u6bb5\u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>JSON \u9519\u8bef\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u89e3\u7801\uff08\u53cd\u5e8f\u5217\u5316\uff09\u6570\u636e\u65f6\uff0c\u5982\u679c JSON \u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6216\u8005\u4e0e\u76ee\u6807\u7ed3\u6784\u4f53\u4e0d\u5339\u914d\uff0cGo \u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    FirstName string `json:\"first_name\"`\n    LastName  string `json:\"last_name\"`\n}\n\nfunc main() {\n    \/\/ \u9519\u8bef\u7684 JSON \u6570\u636e\n    jsonData := `{\"first_name\": \"John\", \"last_name\": 123}`\n\n    var p Person\n    err := json.Unmarshal(&#91;]byte(jsonData), &amp;p)\n    if err != nil {\n        fmt.Println(\"Error unmarshalling JSON:\", err)\n        return\n    }\n\n    fmt.Printf(\"Decoded person: %+v\\n\", p)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Error unmarshalling JSON: invalid character '1' looking for beginning of value<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4e0a\u6240\u793a\uff0c\u5f53 JSON \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\uff08\u4f8b\u5982 <code>\"last_name\": 123<\/code> \u662f\u6570\u5b57\uff0c\u800c\u7ed3\u6784\u4f53\u5b57\u6bb5\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff09\uff0cGo \u4f1a\u8fd4\u56de\u9519\u8bef\uff0c\u63d0\u793a\u7c7b\u578b\u4e0d\u5339\u914d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u5904\u7406\u7a7a\u5b57\u6bb5\u548c\u7f3a\u5931\u5b57\u6bb5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5e8f\u5217\u5316\u65f6\uff0c\u5982\u679c JSON \u6570\u636e\u7f3a\u5c11\u67d0\u4e2a\u5b57\u6bb5\uff0cGo \u4f1a\u6839\u636e\u5b57\u6bb5\u7684\u96f6\u503c\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u4e2d\u7684\u5b57\u6bb5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    FirstName string `json:\"first_name\"`\n    Age       int    `json:\"age\"`\n}\n\nfunc main() {\n    jsonData := `{\"first_name\": \"John\"}`\n\n    var p Person\n    err := json.Unmarshal(&#91;]byte(jsonData), &amp;p)\n    if err != nil {\n        fmt.Println(\"Error unmarshalling JSON:\", err)\n        return\n    }\n\n    fmt.Printf(\"Decoded person: %+v\\n\", p)  \/\/ \u8f93\u51fa\uff1aDecoded person: {FirstName:John Age:0}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>Age<\/code> \u5b57\u6bb5\u6ca1\u6709\u5728 JSON \u6570\u636e\u4e2d\u51fa\u73b0\uff0c\u56e0\u6b64\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u503c <code>0<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5b9e\u9645\u7528\u4f8b\uff1a\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u5b9e\u9645\u7528\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 Go \u4e2d\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>API \u54cd\u5e94\u7684\u53cd\u5e8f\u5217\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a API \u54cd\u5e94\uff0c\u8fd4\u56de\u7684\u662f JSON \u683c\u5f0f\u7684\u6570\u636e\uff0c\u5305\u542b\u7528\u6237\u4fe1\u606f\u548c\u4ed6\u4eec\u7684\u5e16\u5b50\u5217\u8868\u3002\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e2a\u6570\u636e\u53cd\u5e8f\u5217\u5316\u4e3a\u7ed3\u6784\u4f53\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Post struct {\n    ID    int    `json:\"id\"`\n    Title string `json:\"title\"`\n}\n\ntype User struct {\n    ID      int     `json:\"id\"`\n    Name    string  `json:\"name\"`\n    Email   string  `json:\"email\"`\n    Posts   &#91;]Post  `json:\"posts\"`\n}\n\nfunc main() {\n    \/\/ \u6a21\u62df API \u54cd\u5e94\n    apiResponse := `{\n        \"id\": 1,\n        \"name\": \"Alice\",\n        \"email\": \"alice@example.com\",\n        \"posts\": &#91;\n            {\"id\": 101, \"title\": \"Post 1\"},\n            {\"id\": 102, \"title\": \"Post 2\"}\n        ]\n    }`\n\n    var user User\n    err := json.Unmarshal(&#91;]byte(apiResponse), &amp;user)\n    if err != nil {\n        fmt.Println(\"Error unmarshalling JSON:\", err)\n        return\n    }\n\n    fmt.Printf(\"User: %+v\\n\", user)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User: {ID:1 Name:Alice Email:alice@example.com Posts:&#91;{ID:101 Title:Post 1} {ID:102 Title:Post 2}]}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u5b9e\u9645\u7528\u4f8b\u4e2d\uff0c\u6211\u4eec\u6210\u529f\u5730\u5c06 API \u8fd4\u56de\u7684 JSON \u6570\u636e\u53cd\u5e8f\u5217\u5316\u4e3a <code>User<\/code> \u7ed3\u6784\u4f53\u3002\u6ce8\u610f\u5230 <code>Posts<\/code> \u5b57\u6bb5\u662f\u4e00\u4e2a\u5207\u7247\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5305\u542b\u591a\u4e2a <code>Post<\/code> \u7ed3\u6784\u4f53\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u6027\u80fd\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7684 <code>encoding\/json<\/code> \u5305\u867d\u7136\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u4f7f\u7528 json.Decoder<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u5927 JSON \u6570\u636e\uff0c<code>json.Decoder<\/code> \u63d0\u4f9b\u4e86\u6d41\u5f0f\u89e3\u7801\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6574\u4e2a JSON \u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"strings\"\n)\n\nfunc main() {\n    jsonStream := `{\"id\":1, \"name\":\"Alice\"}`\n    decoder := json.NewDecoder(strings.NewReader(jsonStream))\n\n    var data map&#91;string]interface{}\n    if err := decoder.Decode(&amp;data); err != nil {\n        fmt.Println(\"Error decoding JSON:\", err)\n    } else {\n        fmt.Println(data)  \/\/ \u8f93\u51fa\uff1amap&#91;id:1 name:Alice]\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>\u81ea\u5b9a\u4e49 JSON \u7f16\u7801<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>json.Marshaler<\/code> \u548c <code>json.Unmarshaler<\/code> \u63a5\u53e3\uff0c\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65b9\u5f0f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u63a7\u5236\u6570\u636e\u7684\u683c\u5f0f\u548c\u4f18\u5316\u6027\u80fd\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u4e2d\u7684\u5e8f\u5217\u5316\u6d89\u53ca\u7ed3\u6784\u6807\u7b7e\u3001\u9519\u8bef\u5904\u7406\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002\u7ed3\u6784\u6807\u7b7e\u5141\u8bb8\u4f60\u63a7\u5236\u5e8f\u5217\u5316\u65f6\u5b57\u6bb5\u7684\u547d\u540d\u548c\u884c\u4e3a\uff0c\u9519\u8bef\u5904\u7406\u786e\u4fdd\u7a0b\u5e8f\u5728\u9762\u5bf9\u4e0d\u89c4\u8303\u6570\u636e\u65f6\u80fd\u591f\u7a33\u5b9a\u8fd0\u884c\uff0c\u800c\u5b9e\u9645\u7528\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002\u901a\u8fc7\u4f18\u5316\u5e8f\u5217\u5316\u8fc7\u7a0b\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u9ad8\u6548\u3001\u5b89\u5168\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go \u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff08\u6216\u79f0\u7f16\u7801\u548c\u89e3\u7801\uff09\u662f\u5904\u7406\u6570\u636e\u4ea4\u6362\u65f6\u7684\u5173\u952e\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06\u6570\u636e\u7ed3\u6784\u8f6c\u5316\u4e3a\u7279\u5b9a\u683c\u5f0f\uff08\u5982 JSO [&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-998","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/998","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=998"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"predecessor-version":[{"id":999,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/998\/revisions\/999"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}