\u4ee5\u4e0b\u662f\u4e0d\u5e94\u8be5\u9075\u5faa\u6216\u5b9e\u65bd\u7684\u53cd\u6a21\u5f0f\uff1a<\/p>\n
type Animal struct {\r\n Name string\r\n IsMammal bool\r\n}<\/pre>\n\u52a8\u7269\u7c7b\u578b\u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u57fa\u7840\u7c7b\u578b\uff0c\u5b83\u8bd5\u56fe\u5b9a\u4e49\u6240\u6709\u52a8\u7269\u5171\u6709\u7684\u6570\u636e\u3002\u6211\u4e5f\u5c1d\u8bd5\u5411\u52a8\u7269\u63d0\u4f9b\u4e00\u4e9b\u5e38\u89c1\u7684\u884c\u4e3a\u3002<\/p>\n
func (a *Animal) Speak() {\r\n fmt.Println(\"UGH!\",\r\n \"My name is\", a.Name, \", it is\", a.IsMammal, \"I am a mammal\")\r\n}<\/pre>\n\u5927\u591a\u6570\u52a8\u7269\u90fd\u6709\u4ee5\u8fd9\u79cd\u6216\u90a3\u79cd\u65b9\u5f0f\u8bf4\u8bdd\u7684\u80fd\u529b\u3002\u7136\u800c\uff0c\u8bd5\u56fe\u5c06\u8fd9\u79cd\u5e38\u89c1\u7684\u884c\u4e3a\u4ec5\u4ec5\u9002\u7528\u4e8e\u4e00\u79cd\u52a8\u7269\u5e76\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u6211\u4e0d\u77e5\u9053\u8fd9\u79cd\u52a8\u7269\u53d1\u51fa\u4ec0\u4e48\u58f0\u97f3\uff0c\u6240\u4ee5\u6211\u5199\u4e86 UGH<\/p>\n
type Dog struct {\r\n Animal\r\n PackFactor int\r\n}<\/pre>\n\u73b0\u5728\uff0c\u771f\u6b63\u7684\u95ee\u9898\u5f00\u59cb\u4e86\u3002\u6211\u8bd5\u56fe\u7528\u5d4c\u5165\u7684\u65b9\u5f0f\u4f7f\u72d7\u62e5\u6709\u52a8\u7269\u7684\u4e00\u5207\uff0c\u800c\u4e14\u66f4\u591a\u3002\u4ece\u8868\u9762\u4e0a\u770b\uff0c\u8fd9\u4f3c\u4e4e\u662f\u53ef\u884c\u7684\uff0c\u4f46\u4e5f\u4f1a\u6709\u95ee\u9898\u3002\u65e2\u7136\u5982\u6b64\uff0c\u72d7\u786e\u5b9e\u6709\u4e00\u79cd\u7279\u6b8a\u7684\u8bf4\u8bdd\u65b9\u5f0f<\/p>\n
func (d *Dog) Speak() {\r\n fmt.Println(\"Woof!\",\r\n \"My name is\", d.Name,\r\n \", it is\", d.IsMammal,\r\n \"I am a mammal with a pack factor of\", d.PackFactor)\r\n}<\/pre>\n\u5728\u8bf4\u8bdd\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u53ef\u4ee5\u628a UGH \u6362\u6210 Woof \u3002\u8fd9\u5c31\u662f \u5177\u4f53\u5230\u72d7\u7684\u8bf4\u8bdd\u65b9\u5f0f<\/p>\n
type Cat struct {\r\n Animal\r\n ClimbFactor int\r\n}<\/pre>\n\u5982\u679c\u6211\u6253\u7b97\u517b\u4e00\u53ea\u4ee3\u8868\u52a8\u7269\u7684\u72d7\uff0c\u90a3\u4e48\u6211\u5c31\u5fc5\u987b\u517b\u4e00\u53ea\u732b\u3002\u4f7f\u7528\u5d4c\u5165\u6cd5\uff0c\u732b\u662f\u52a8\u7269\u7684\u4e00\u5207\uff0c\u800c\u4e14\u66f4\u591a\u3002<\/p>\n
func (c *Cat) Speak() {\r\n fmt.Println(\"Meow!\",\r\n \"My name is\", c.Name,\r\n \", it is\", c.IsMammal,\r\n \"I am a mammal with a climb factor of\", c.ClimbFactor)\r\n}<\/pre>\n\u5728\u5b9e\u73b0 \u200b\u200bSpeak\u200b\u200b \u7684\u65b9\u6cd5\u4e2d\uff0c\u6211\u53ef\u4ee5\u628a UGH \u6539\u6210 Meow \u3002\u8fd9\u5c31\u662f\u5177\u4f53\u5230\u732b\u7684\u8bf4\u8bdd\u65b9\u5f0f \u4e00\u5207\u770b\u8d77\u6765\u90fd\u5f88\u597d\uff0c\u770b\u8d77\u6765\u5d4c\u5165\u63d0\u4f9b\u7684\u529f\u80fd\u4e0e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u7ee7\u627f\u76f8\u540c\u3002\u7136\u540e\u6211\u8bd5\u7740\u628a\u72d7\u548c\u732b\u5206\u7ec4\uff0c\u56e0\u4e3a\u5b83\u4eec\u6709\u5171\u540c\u7684DNA\uff0c\u90fd\u662f\u52a8\u7269\u3002<\/p>\n
\/\/ This does not compile, a Dog and Cat are not an Animal.\r\nanimals := []Animal{\r\n Dog{\r\n Animal: Animal{\r\n Name: \"Fido\",\r\n IsMammal: true,\r\n },\r\n PackFactor: 5,\r\n },\r\n Cat{\r\n Animal: Animal{\r\n Name: \"Milo\",\r\n IsMammal: true,\r\n },\r\n ClimbFactor: 4,\r\n },\r\n}\r\n\r\nfor _, animal := range animals {\r\n animal.Speak()\r\n}<\/pre>\n\u5f53\u6211\u5c1d\u8bd5\u8fd9\u6837\u505a\u65f6\uff0c\u7f16\u8bd1\u5668\u62b1\u6028\u72d7\u548c\u732b\u4e0d\u662f\u52a8\u7269\uff0c\u8fd9\u662f\u771f\u7684\u3002 \u5d4c\u5165\u4e0e\u7ee7\u627f\u4e0d\u540c\uff0c\u8fd9\u662f\u6211\u9700\u8981\u8fdc\u79bb\u7684\u6a21\u5f0f\u3002 \u72d7\u662f\u72d7\uff0c\u732b\u662f\u732b\uff0c\u52a8\u7269\u662f\u52a8\u7269\u3002 \u6211\u4e0d\u80fd\u628a\u72d7\u548c\u732b\u5f53\u4f5c\u52a8\u7269\u6765\u4f20\u9012\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u662f\u3002 \u8fd9\u79cd\u673a\u5236\u4e5f\u4e0d\u662f\u5f88\u7075\u6d3b\u3002 \u5b83\u9700\u8981\u5f00\u53d1\u4eba\u5458\u8fdb\u884c\u914d\u7f6e\uff0c\u9664\u975e\u6211\u53ef\u4ee5\u8bbf\u95ee\u4ee3\u7801\u5e76\u4e14\u53ef\u4ee5\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u8fdb\u884c\u914d\u7f6e\u66f4\u6539\uff0c\u5426\u5219\u8fd9\u4e0d\u662f\u5f88\u7075\u6d3b \u5982\u679c\u8fd9\u4e0d\u662f\u6211\u4eec\u6784\u5efa Dog \u548c Cat \u96c6\u5408\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u5982\u4f55\u5728 Go \u4e2d\u505a\u5230\u8fd9\u4e00\u70b9\uff1f \u8fd9\u4e0d\u662f\u901a\u8fc7\u5171\u540c\u7684 DNA \u8fdb\u884c\u5206\u7ec4\uff0c\u800c\u662f\u901a\u8fc7\u5171\u540c\u7684\u884c\u4e3a\u8fdb\u884c\u5206\u7ec4\u3002 \u884c\u4e3a\u662f\u5173\u952e\u3002<\/p>\n
type Speaker interface {\r\n Speak()\r\n}<\/pre>\n\u5982\u679c\u6211\u4f7f\u7528\u4e00\u4e2a\u63a5\u53e3\uff0c\u90a3\u4e48\u6211\u53ef\u4ee5\u5b9a\u4e49\u6211\u60f3\u8981\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7684\u901a\u7528\u884c\u4e3a\u65b9\u6cd5\u96c6:<\/p>\n
speakers := []Speaker{\r\n &Dog{\r\n Animal: Animal{\r\n Name: \"Fido\",\r\n IsMammal: true,\r\n },\r\n PackFactor: 5,\r\n },\r\n &Cat{\r\n Animal: Animal{\r\n Name: \"Milo\",\r\n IsMammal: true,\r\n },\r\n ClimbFactor: 4,\r\n },\r\n}\r\n\r\nfor _, speaker := range speakers {\r\n speaker.Speak()\r\n}<\/pre>\n\u5728\u65b0\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u73b0\u5728\u53ef\u4ee5\u6839\u636e\u72d7\u548c\u732b\u7684\u5171\u540c\u884c\u4e3a\u96c6\u5c06\u5b83\u4eec\u5206\u7ec4\u3002\u7684\u884c\u4e3a\uff0c\u4e5f\u5c31\u662f\u72d7\u548c\u732b\u80fd\u8bf4\u8bdd\u7684\u4e8b\u5b9e\uff0c\u628a\u5b83\u4eec\u5f52\u4e3a\u4e00\u7c7b\u3002 \u4e8b\u5b9e\u4e0a\uff0cAnimal \u7c7b\u578b\u786e\u5b9e\u662f\u7c7b\u578b\u6c61\u67d3\uff0c\u56e0\u4e3a\u58f0\u660e\u4e00\u4e2a\u7c7b\u578b\u53ea\u662f\u4e3a\u4e86\u5171\u4eab\u4e00\u7ec4\u516c\u5171\u72b6\u6001\u662f\u4e00\u79cd\u65b9\u5f0f\uff0c\u5e94\u8be5\u907f\u514d.<\/p>\n
type Dog struct {\r\n Name string\r\n IsMammal bool\r\n PackFactor int\r\n}\r\ntype Cat struct {\r\n Name string\r\n IsMammal bool\r\n ClimbFactor int\r\n}<\/pre>\n\u5728\u8fd9\u79cd\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6211\u5b81\u613f\u770b\u5230\u52a8\u7269\u7c7b\u578b\u88ab\u5220\u9664\uff0c\u5b57\u6bb5\u88ab\u590d\u5236\u5e76\u7c98\u8d34\u5230\u72d7\u548c\u732b\u7c7b\u578b\u4e2d\u3002\u7a0d\u540e\u6211\u5c06\u6709\u5173\u4e8e\u66f4\u597d\u7684\u6a21\u5f0f\u7684\u8bf4\u660e\uff0c\u4ee5\u6d88\u9664\u8fd9\u4e9b\u60c5\u51b5\u7684\u53d1\u751f\u3002 \u4ee5\u4e0b\u662f\u539f\u59cb\u4ee3\u7801\u4e2d\u7684\u4ee3\u7801\u5f02\u7c7b\uff1a<\/p>\n
Animal \u7c7b\u578b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u91cd\u7528\u72b6\u6001\u7684\u62bd\u8c61\u5c42\u3002<\/p>\n
\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u9700\u8981\u521b\u5efa\u6216\u5355\u72ec\u4f7f\u7528 Animal \u7c7b\u578b\u7684\u503c\u3002<\/p>\n
\u6cdb\u5316\u4e86 Animal \u7c7b\u578b\u7684 Speak \u65b9\u6cd5\u7684\u5b9e\u73b0\u3002<\/p>\n
\u6c38\u8fdc\u4e0d\u4f1a\u8c03\u7528 Animal \u7c7b\u578b\u7684 Speak \u65b9\u6cd5\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\n\u5173\u4e8e\u58f0\u660e\u7c7b\u578b\u7684\u6307\u5357\uff1a<\/p>\n
\u58f0\u660e\u4ee3\u8868\u65b0\u4e8b\u7269\u6216\u72ec\u7279\u4e8b\u7269\u7684\u7c7b\u578b\u3002<\/p>\n
\u4e0d\u8981\u4ec5\u4ec5\u4e3a\u4e86\u53ef\u8bfb\u6027\u800c\u521b\u5efa\u522b\u540d\u3002<\/p>\n
\u9a8c\u8bc1\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u662f\u5355\u72ec\u521b\u5efa\u6216\u4f7f\u7528\u7684\u3002<\/p>\n
\u5d4c\u5165\u7c7b\u578b\u4e0d\u662f\u56e0\u4e3a\u6211\u9700\u8981\u72b6\u6001\uff0c\u800c\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u884c\u4e3a\u3002<\/p>\n
\u5982\u679c\u6211\u4e0d\u8003\u8651\u884c\u4e3a\uff0c\u6211\u5c31\u4f1a\u5c06\u81ea\u5df1\u9501\u5b9a\u5728\u672a\u6765\u4e0d\u8fdb\u884c\u7ea7\u8054\u4ee3\u7801\u66f4\u6539\u5c31\u65e0\u6cd5\u6210\u957f\u7684\u8bbe\u8ba1\u4e2d\u3002<\/p>\n
\u4f5c\u4e3a\u73b0\u6709\u7c7b\u578b\u7684\u522b\u540d\u6216\u62bd\u8c61\u7684\u95ee\u9898\u7c7b\u578b\u3002<\/p>\n
\u552f\u4e00\u76ee\u7684\u662f\u5171\u4eab\u516c\u5171\u72b6\u6001\u7684\u95ee\u9898\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4ee5\u4e0b\u662f\u4e0d\u5e94\u8be5\u9075\u5faa\u6216\u5b9e\u65bd\u7684\u53cd\u6a21\u5f0f\uff1a type Animal struct { Name string IsMam […]<\/p>\n","protected":false},"author":1479,"featured_media":153204,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-233373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233373"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233373\/revisions"}],"predecessor-version":[{"id":233377,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233373\/revisions\/233377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153204"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}