{"id":3152,"date":"2025-03-25T23:32:40","date_gmt":"2025-03-25T15:32:40","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3152"},"modified":"2025-03-25T23:32:40","modified_gmt":"2025-03-25T15:32:40","slug":"go-%e8%af%ad%e8%a8%80%e7%bb%93%e6%9e%84%ef%bc%88struct%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e7%bb%93%e6%9e%84%ef%bc%88struct%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Go \u8bed\u8a00\u7ed3\u6784\uff08Struct\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<code>struct<\/code>\uff08\u7ed3\u6784\u4f53\uff09\u662f\u4e00\u79cd\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u5b83\u7c7b\u4f3c\u4e8e C \u8bed\u8a00\u4e2d\u7684 <code>struct<\/code> \u6216 Java \u4e2d\u7684 <code>class<\/code>\uff08\u4f46 Go \u8bed\u8a00\u6ca1\u6709\u7c7b\u7684\u6982\u5ff5\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u7ed3\u6784\u4f53\u7684\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u4f7f\u7528 <code>type<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7ed3\u6784\u4f53\uff0c<code>struct<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u7ed3\u6784\u4f53\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\ntype Person struct {\n    Name string\n    Age  int\n    Address string\n}\n\nfunc main() {\n    \/\/ \u521b\u5efa\u7ed3\u6784\u4f53\u5b9e\u4f8b\n    p := Person{\"Alice\", 25, \"Beijing\"}\n    fmt.Println(p)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{Alice 25 Beijing}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u7ed3\u6784\u4f53\u7684\u521d\u59cb\u5316<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u652f\u6301\u591a\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u4f7f\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p1 := Person{\"Bob\", 30, \"Shanghai\"} \/\/ \u987a\u5e8f\u5339\u914d\u5b57\u6bb5\np2 := Person{Name: \"Tom\", Age: 28}  \/\/ \u4f7f\u7528\u5b57\u6bb5\u540d\u521d\u59cb\u5316\uff08\u90e8\u5206\u5b57\u6bb5\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4f7f\u7528 new \u5173\u952e\u5b57<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p3 := new(Person) \/\/ \u8fd4\u56de\u6307\u9488 *Person\uff0c\u5b57\u6bb5\u9ed8\u8ba4\u503c\np3.Name = \"Mike\"\np3.Age = 22<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff083\uff09\u4f7f\u7528 <code>&amp;<\/code> \u76f4\u63a5\u521b\u5efa\u6307\u9488<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p4 := &amp;Person{\"Emma\", 26, \"Guangzhou\"} \/\/ \u76f4\u63a5\u8fd4\u56de\u6307\u9488<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u7ed3\u6784\u4f53\u5b57\u6bb5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u533f\u540d\u5b57\u6bb5<\/strong><\/h3>\n\n\n\n<p>Go \u5141\u8bb8\u5b9a\u4e49\u6ca1\u6709\u5b57\u6bb5\u540d\u79f0\u7684\u7ed3\u6784\u4f53\uff08\u533f\u540d\u5b57\u6bb5\uff09\uff0c\u8be5\u5b57\u6bb5\u7684\u7c7b\u578b\u5373\u4e3a\u540d\u79f0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type User struct {\n    string\n    int\n}\n\nu := User{\"John\", 25}\nfmt.Println(u.string, u.int) \/\/ \u8bbf\u95ee\u65b9\u5f0f<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u5d4c\u5957\u7ed3\u6784\u4f53<\/strong><\/h3>\n\n\n\n<p>\u7ed3\u6784\u4f53\u53ef\u4ee5\u5d4c\u5957\u53e6\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Address struct {\n    City  string\n    State string\n}\n\ntype Employee struct {\n    Name    string\n    Age     int\n    Address \/\/ \u533f\u540d\u5d4c\u5957\n}\n\ne := Employee{\"Jack\", 35, Address{\"New York\", \"NY\"}}\nfmt.Println(e.City) \/\/ \u76f4\u63a5\u8bbf\u95ee Address \u7684\u5b57\u6bb5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u7ed3\u6784\u4f53\u7684\u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<p>Go \u5141\u8bb8\u5728\u7ed3\u6784\u4f53\u4e0a\u5b9a\u4e49\u65b9\u6cd5\uff08\u65b9\u6cd5\u662f\u5e26\u6709\u63a5\u6536\u8005\u7684\u51fd\u6570\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Rectangle struct {\n    Width, Height float64\n}\n\n\/\/ \u5b9a\u4e49\u65b9\u6cd5\nfunc (r Rectangle) Area() float64 {\n    return r.Width * r.Height\n}\n\nrect := Rectangle{10, 5}\nfmt.Println(rect.Area()) \/\/ 50<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u6307\u9488\u63a5\u6536\u8005<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528\u6307\u9488\u63a5\u6536\u8005\u53ef\u4ee5\u4fee\u6539\u7ed3\u6784\u4f53\u5185\u90e8\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (r *Rectangle) Scale(factor float64) {\n    r.Width *= factor\n    r.Height *= factor\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837 <code>Scale<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4fee\u6539 <code>Rectangle<\/code> \u5b9e\u4f8b\u7684 <code>Width<\/code> \u548c <code>Height<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u7ed3\u6784\u4f53\u7684 JSON \u5904\u7406<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u7684 <code>encoding\/json<\/code> \u5305\u5141\u8bb8\u7ed3\u6784\u4f53\u4e0e JSON \u4e92\u8f6c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Product struct {\n    Name  string  `json:\"name\"`\n    Price float64 `json:\"price\"`\n}\n\nfunc main() {\n    p := Product{\"Laptop\", 1299.99}\n    jsonData, _ := json.Marshal(p)\n    fmt.Println(string(jsonData)) \/\/ {\"name\":\"Laptop\",\"price\":1299.99}\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09JSON \u53cd\u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var p2 Product\njson.Unmarshal(jsonData, &amp;p2)\nfmt.Println(p2.Name, p2.Price)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u7ed3\u6784\u4f53\u7684\u6bd4\u8f83<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u6784\u4f53\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83\uff08\u6240\u6709\u5b57\u6bb5\u53ef\u6bd4\u8f83\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p1 := Person{\"Alice\", 25, \"Beijing\"}\np2 := Person{\"Alice\", 25, \"Beijing\"}\nfmt.Println(p1 == p2) \/\/ true<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u7ed3\u6784\u4f53\u4e0e\u63a5\u53e3<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u7684\u63a5\u53e3\u53ef\u4ee5\u7528\u4e8e\u7ed3\u6784\u4f53\uff0c\u5b9e\u73b0\u591a\u6001\u7279\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Shape interface {\n    Area() float64\n}\n\ntype Circle struct {\n    Radius float64\n}\n\nfunc (c Circle) Area() float64 {\n    return 3.14 * c.Radius * c.Radius\n}\n\nfunc main() {\n    var s Shape = Circle{10}\n    fmt.Println(s.Area()) \/\/ 314\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u7ed3\u6784\u4f53\u6807\u7b7e\uff08Struct Tags\uff09<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u6784\u4f53\u5b57\u6bb5\u53ef\u4ee5\u4f7f\u7528\u6807\u7b7e\uff08Tag\uff09\u5b58\u50a8\u989d\u5916\u4fe1\u606f\uff08\u5e38\u7528\u4e8e JSON \u89e3\u6790\u3001\u6570\u636e\u5e93 ORM\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type User struct {\n    ID   int    `json:\"id\"`\n    Name string `json:\"name,omitempty\"`\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>json:\"name,omitempty\"<\/code> \u8868\u793a\u5982\u679c <code>Name<\/code> \u4e3a\u7a7a\uff0c\u5219 JSON \u4e2d\u4e0d\u663e\u793a\u8be5\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u7ed3\u6784\u4f53\u4e0e\u5185\u5b58\u5e03\u5c40<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u4f1a\u6309\u7167\u58f0\u660e\u987a\u5e8f\u6392\u5217\uff0cGo \u4f1a\u8fdb\u884c<strong>\u5185\u5b58\u5bf9\u9f50<\/strong>\u4f18\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type A struct {\n    a int8  \/\/ 1 \u5b57\u8282\n    b int64 \/\/ 8 \u5b57\u8282\n    c int8  \/\/ 1 \u5b57\u8282\n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4e86\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u53ef\u4ee5\u8c03\u6574\u5b57\u6bb5\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type B struct {\n    a int8  \/\/ 1 \u5b57\u8282\n    c int8  \/\/ 1 \u5b57\u8282\n    b int64 \/\/ 8 \u5b57\u8282\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. \u53c2\u8003\u8d44\u6599<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/golang.org\/doc\/effective_go.html#structs\">Go Struct<\/a><\/li>\n\n\n\n<li>JSON \u5e8f\u5217\u5316\uff1a<a href=\"https:\/\/pkg.go.dev\/encoding\/json\">Go encoding\/json<\/a><\/li>\n\n\n\n<li>Go \u8bed\u8a00\u63a5\u53e3\uff1a<a href=\"https:\/\/golang.org\/doc\/effective_go.html#interfaces\">Go Interfaces<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0cstruct\uff08\u7ed3\u6784\u4f53\uff09\u662f\u4e00\u79cd\u590d\u5408\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u5b83\u7c7b\u4f3c\u4e8e C  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3153,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3152","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=3152"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3152\/revisions"}],"predecessor-version":[{"id":3154,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3152\/revisions\/3154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3153"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}