{"id":75666,"date":"2024-03-01T15:50:28","date_gmt":"2024-03-01T07:50:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=75666"},"modified":"2024-03-01T15:50:43","modified_gmt":"2024-03-01T07:50:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e8%87%aa%e5%ae%9a%e4%b9%89%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/75666.html","title":{"rendered":"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-75667\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/03\/generated_00-16.png?x-oss-process=image\/auto-orient,1\/format,webp\" width=\"600\" height=\"342\" alt=\"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\" \/><\/p>\n<p>\u5f00\u5934\u6bb5\u843d\uff1a<\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c<strong>\u9996\u5148\u5b9a\u4e49\u7c7b\u578b\u7684\u57fa\u7840\u7ed3\u6784\u3001\u5176\u6b21\u5b9e\u73b0\u6240\u9700\u7684\u65b9\u6cd5\u3001\u6700\u540e\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\u6765\u4f7f\u7528\u65b0\u7c7b\u578b<\/strong>\u3002\u4f8b\u5982\uff0c\u4f7f\u7528type\u5173\u952e\u5b57\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u57fa\u4e8e\u73b0\u6709\u7684\u6570\u636e\u7c7b\u578b\u6216\u7ed3\u6784\u3002<strong>\u5b9a\u4e49\u7c7b\u578b\u7684\u57fa\u7840\u7ed3\u6784<\/strong>\u5bf9\u4e8e\u521b\u5efa\u4e00\u4e2a\u7d27\u51d1\u4e14\u9ad8\u6548\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u6765\u8bf4\u5c24\u4e3a\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u80fd\u9700\u8981\u4e00\u4e2a`type Celsius float64`\u6765\u7279\u522b\u8868\u793a\u6444\u6c0f\u5ea6\u6e29\u5ea6\uff0c\u800c\u4e0d\u662f\u666e\u901a\u7684`float64`\u3002\u8fd9\u6837\u505a\u9664\u4e86\u63d0\u4f9b\u66f4\u660e\u786e\u7684\u7c7b\u578b\u5b9a\u4e49\uff0c\u8fd8\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b0\u7c7b\u578b\u4e0a\u9644\u52a0\u4e13\u7528\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u4f7f\u7c7b\u578b\u66f4\u52a0\u4e30\u5bcc\u548c\u6709\u8868\u73b0\u529b\u3002<\/p>\n<p>\u6b63\u6587\uff1a<\/p>\n<h2><strong>\u4e00\u3001\u57fa\u672c\u7c7b\u578b\u5b9a\u4e49<\/strong><\/h2>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u901a\u5e38\u7528\u6765\u5b9e\u73b0\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u548c\u884c\u4e3a\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u4f60\u9996\u5148\u9700\u8981\u4f7f\u7528`type`\u5173\u952e\u5b57\u58f0\u660e\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u540d\u79f0\uff0c\u5e76\u6307\u5b9a\u5b83\u7684\u57fa\u7840\u7c7b\u578b\u3002\u8fd9\u4e2a\u57fa\u7840\u7c7b\u578b\u53ef\u4ee5\u662fGo\u7684\u4efb\u4f55\u5185\u5efa\u7c7b\u578b\uff0c\u4f8b\u5982int\u3001float64\u3001string\uff0c\u751a\u81f3\u662f\u590d\u6742\u7684struct\u6216\u8005interface\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u81ea\u5b9a\u4e49\u7b80\u5355\u7684\u6570\u503c\u7c7b\u578b<\/strong><\/p>\n<p>&#8220;`go<\/p>\n<p>type Distance int<\/p>\n<p>type Kilogram float64<\/p>\n<p>type Title string<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0cDistance\u3001Kilogram\u548cTitle\u90fd\u662f\u57fa\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u7684\u65b0\u7c7b\u578b\u3002\u867d\u7136\u4ed6\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u8868\u793a\u4e0e\u539f\u59cb\u7c7b\u578b\u76f8\u540c\uff0c\u4f46\u662f\u4f5c\u4e3a\u72ec\u7acb\u7684\u7c7b\u578b\uff0c\u4ed6\u4eec\u53ef\u4ee5\u62e5\u6709\u81ea\u5df1\u7684\u65b9\u6cd5\u96c6\u3002<\/p>\n<h2><strong>\u4e8c\u3001\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49<\/strong><\/h2>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b\u578b<\/strong>\u662fGO\u8bed\u8a00\u4e2d\u6700\u5f3a\u5927\u7684\u7c7b\u578b\u5b9a\u4e49\u65b9\u5f0f\u4e4b\u4e00\u3002\u7ed3\u6784\u4f53\u5141\u8bb8\u4f60\u5c06\u591a\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u9879\u7ec4\u5408\u6210\u4e00\u4e2a\u903b\u8f91\u5355\u5143\u3002\u8fd9\u5bf9\u4e8e\u6a21\u5757\u5316\u548c\u6570\u636e\u7ba1\u7406\u975e\u5e38\u6709\u7528\u3002\u7ed3\u6784\u4f53\u4e2d\u53ef\u4ee5\u5305\u542b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u7ed3\u6784\u4f53\u6216\u63a5\u53e3\u3002<\/p>\n<p>&#8220;`go<\/p>\n<p>type Employee struct {<\/p>\n<p>    ID        int<\/p>\n<p>    FirstName string<\/p>\n<p>    LastName  string<\/p>\n<p>    Position  string<\/p>\n<p>    Salary    float64<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c`Employee`\u7ed3\u6784\u4f53\u4ee3\u8868\u4e86\u5458\u5de5\u4fe1\u606f\u7684\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u5b83\u5c06\u5458\u5de5\u7684\u5404\u4e2a\u5c5e\u6027\u7ec4\u7ec7\u5728\u4e86\u4e00\u8d77\uff0c\u65b9\u4fbf\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4f4d\u8fdb\u884c\u7ba1\u7406\u548c\u4f7f\u7528\u3002<\/p>\n<h2><strong>\u4e09\u3001\u65b9\u6cd5\u7684\u5b9e\u73b0<\/strong><\/h2>\n<p>\u4e3a\u4e86\u8ba9\u81ea\u5b9a\u4e49\u7c7b\u578b\u66f4\u52a0\u6709\u7528\u548c\u5f3a\u5927\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5176<strong>\u5b9e\u73b0\u65b9\u6cd5<\/strong>\u3002\u65b9\u6cd5\u662f\u9644\u52a0\u5230\u7279\u5b9a\u7c7b\u578b\u4e0a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5728\u8be5\u7c7b\u578b\u7684\u5b9e\u4f8b\u4e0a\u8c03\u7528\u3002\u901a\u8fc7\u7ed9\u81ea\u5b9a\u4e49\u7c7b\u578b\u6dfb\u52a0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u7c7b\u578b\u66f4\u52a0\u529f\u80fd\u5316\u3001\u5177\u6709\u5bf9\u8c61\u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u6dfb\u52a0\u65b9\u6cd5\u7684\u4f8b\u5b50<\/strong>\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>func (d Distance) String() string {<\/p>\n<p>    return fmt.<a href=\"https:\/\/docs.pingcode.com\/agile\/scrum\/sprint\" target=\"_blank\">Sprint<\/a>f(&#8220;%d meters&#8221;, d)<\/p>\n<p>}<\/p>\n<p>func (e *Employee) FullName() string {<\/p>\n<p>    return e.FirstName + &#8221; &#8221; + e.LastName<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u91cc\uff0cDistance\u7c7b\u578b\u6709\u4e86\u4e00\u4e2aString\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u8be5\u8ddd\u79bb\u7684\u6587\u672c\u63cf\u8ff0\uff1bEmployee\u7c7b\u578b\u5219\u6709\u4e86\u4e00\u4e2aFullName\u65b9\u6cd5\uff0c\u7528\u6765\u83b7\u53d6\u5458\u5de5\u7684\u5168\u540d\u3002<\/p>\n<h2><strong>\u56db\u3001\u63a5\u53e3\u7684\u4f7f\u7528<\/strong><\/h2>\n<p>\u63a5\u53e3\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u578b\u65f6\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\u3002<strong>\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u4e2a\u884c\u4e3a\u7684\u96c6\u5408<\/strong>\uff1b\u4efb\u4f55\u5177\u6709\u8fd9\u4e9b\u884c\u4e3a\u7684\u7c7b\u578b\u90fd\u53ef\u4ee5\u8bf4\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u3002\u8fd9\u5141\u8bb8Go\u8bed\u8a00\u7684\u7a0b\u5e8f\u5458\u4ee5\u4e00\u79cd\u7075\u6d3b\u548c\u89e3\u8026\u7684\u65b9\u5f0f\u4f7f\u7528\u4ed6\u4eec\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002<\/p>\n<p><strong>\u8bbe\u8ba1\u517c\u5bb9\u63a5\u53e3\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b<\/strong>\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>type Rater interface {<\/p>\n<p>    Rate() float64<\/p>\n<p>}<\/p>\n<p>func (e *Employee) Rate() float64 {<\/p>\n<p>    return e.Salary \/ 1000<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<h2><strong>\u4e94\u3001\u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u578b\u3002\u8fd9\u5728Go\u8bed\u8a00\u4e2d\u662f\u5141\u8bb8\u7684\uff0c\u4f46\u5fc5\u987b\u660e\u786e\u8fdb\u884c\u3002<strong>\u7c7b\u578b\u8f6c\u6362\u662f\u663e\u5f0f\u7684<\/strong>\uff0c\u9700\u8981\u7a0b\u5e8f\u5458\u663e\u793a\u5730\u6307\u51fa\u8f6c\u6362\u7684\u76ee\u6807\u7c7b\u578b\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\u7684\u4f8b\u5b50<\/strong>\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>var d Distance = 5<\/p>\n<p>var meters int = int(d) \/\/ \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>&#8220;`<\/p>\n<p>\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u4f7f\u5f97\u4e24\u4e2a\u64cd\u4f5c\u6570\u80fd\u5728\u903b\u8f91\u4e0a\u76f8\u4e92\u64cd\u4f5c\uff0c\u4f46\u5e94\u6ce8\u610f\uff0c\u4e0d\u6070\u5f53\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<h2><strong>\u516d\u3001\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p>\u5728Go\u8bed\u8a00\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<strong>\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u590d\u6742\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u5c01\u88c5<\/strong>\u3002\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6574\u4f53\u7ed3\u6784\u548c\u8d28\u91cf\u3002<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1f<\/strong><\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528`type`\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u5b9a\u4e49\u7ed3\u6784\u4f53\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u7ed3\u6784\u4f53\u4e2d\u5305\u542b\u7684\u5b57\u6bb5\u548c\u5b83\u4eec\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<br \/>\n&#8220;`go<br \/>\ntype Person struct {<br \/>\n    Name string<br \/>\n    Age  int<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u6dfb\u52a0\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u63a5\u6536\u8005(receiver)\u6765\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u578b\u6dfb\u52a0\u65b9\u6cd5\u3002\u63a5\u6536\u8005\u53ef\u4ee5\u662f\u6307\u9488\u7c7b\u578b\u6216\u503c\u7c7b\u578b\u3002\u4f7f\u7528\u6307\u9488\u7c7b\u578b\u4f5c\u4e3a\u63a5\u6536\u8005\u53ef\u4ee5\u4fee\u6539\u63a5\u6536\u8005\u672c\u8eab\uff0c\u800c\u503c\u7c7b\u578b\u5219\u662f\u63a5\u6536\u8005\u7684\u4e00\u4efd\u526f\u672c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<br \/>\n&#8220;`go<br \/>\nfunc (p *Person) SayHello() {<br \/>\n    fmt.Printf(&#8220;Hello, my name is %s and I am %d years old.\\n&#8221;, p.Name, p.Age)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u578b\u5b9e\u73b0\u63a5\u53e3\uff1f<\/strong><\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0\u63a5\u53e3\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u8981\u786e\u4fdd\u81ea\u5b9a\u4e49\u7c7b\u578b\u62e5\u6709\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u5373\u53ef\u5b9e\u73b0\u8be5\u63a5\u53e3\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u63a5\u53e3`Animal`\u5b9a\u4e49\u4e86`Speak()`\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9`Person`\u7c7b\u578b\u5b9e\u73b0\u8be5\u63a5\u53e3\uff1a<br \/>\n&#8220;`go<br \/>\ntype Animal interface {<br \/>\n    Speak()<br \/>\n}<\/p>\n<p>func (p Person) Speak() {<br \/>\n    fmt.Println(&#8220;I am a person.&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a \u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u9996\u5148\u5b9a\u4e49\u7c7b\u578b\u7684\u57fa\u7840\u7ed3\u6784\u3001\u5176\u6b21\u5b9e\u73b0\u6240\u9700\u7684\u65b9\u6cd5\u3001\u6700\u540e\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\u6765\u4f7f\u7528\u65b0\u7c7b [&hellip;]","protected":false},"author":1,"featured_media":75669,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75666"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=75666"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75666\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/75669"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=75666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=75666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=75666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}