{"id":43134,"date":"2024-12-01T09:24:14","date_gmt":"2024-12-01T01:24:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43134\/"},"modified":"2024-12-01T09:24:14","modified_gmt":"2024-12-01T01:24:14","slug":"go%e3%80%81%e4%b8%80%e6%b5%81%e7%9a%84%e5%8a%9f%e8%83%bd%e5%92%8c%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43134\/","title":{"rendered":"Go\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Go\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5<\/span><\/p>\n<h1>Go\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 21:18:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aGo\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u8fc7\u53bb\u7684\u51e0\u5929\u91cc\uff0c\u6211\u4e00\u76f4\u5728\u5bfb\u627e\u89e3\u51b3\u4e00\u6d41\u51fd\u6570\u95ee\u9898\u7684\u6700\u4f73\u65b9\u6cd5\uff08\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u5206\u914d\u7ed9\u67d0\u4e2a\u53d8\u91cf\uff09\u4ee5\u53ca\u6548\u7387\u65b9\u9762\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u6b63\u5728\u7f16\u5199 yugioh \u7eb8\u724c\u6e38\u620f\uff0c\u5e76\u4e14\u6211\u5e0c\u671b\u6bcf\u5f20\u7eb8\u724c\u7c7b\u578b\u7684\u7eb8\u724c\u81f3\u5c11\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\uff1a<\/p>\n<pre>type card struct {\n    name string\n    text string\n}<\/pre>\n<p>\u6211\u4e00\u76f4\u5728\u601d\u8003\u5728\u54ea\u91cc\uff08\u4ee5\u53ca\u5982\u4f55\uff09\u5bf9\u6bcf\u5f20\u5361\u7684\u5355\u72ec\u529f\u80fd\u8fdb\u884c\u7f16\u7a0b\u7684\u60f3\u6cd5\u3002\u6211\u76ee\u524d\u786e\u4fe1\uff0c\u7b2c\u4e00\u7c7b\u51fd\u6570\u7684\u6700\u4f73\u4f4d\u7f6e\u662f\u5728 <code>type \u5361 struct<\/code> \u4e2d\uff0c\u5e76\u50cf\u5728 python \u4e2d\u4e00\u6837\u5c06\u65b0\u5c5e\u6027\u521b\u5efa\u4e3a\u201c\u53ef\u8c03\u7528\u201d\uff08\u8f6c\u5230\u6f14\u793a\u94fe\u63a5\uff09\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\n\ntype card struct {\n    name string\n    text string\n    f interface{}\n}\n\ntype monsterCard struct {\n    card\n    attack int\n    defense int\n}\n\ntype buff func(target *monsterCard) \/\/ Could be defined in a second file\n\ntype swap func(target *monsterCard, value int) \/\/ ditto\n\nvar increaseAttack buff = func(target *monsterCard)  { \/\/ ditto\n    target.attack += 100\n}\n\nvar swichStats swap = func(target *monsterCard, value int) { \/\/ ditto\n    attack := target.attack\n    target.attack = value\n    target.defense = attack\n}\n\nfunc main()  {\n    m1 := monsterCard{\n        card:    card{\n            name: \"Celtic Guardian\",\n            f:    increaseAttack,\n        },\n        attack:  1400,\n        defense: 1200,\n    }\n    m2 := monsterCard{\n        card:    card{\n            name:     \"Dark Magician\",\n            f:         swichStats,\n        },\n        attack:  2500,\n        defense: 2100,\n    }\n    var monsters = [2]monsterCard{m1, m2}\n    for _, m := range monsters {\n        fmt.Println(m)\n        switch m.f.(type) {\n        case buff:\n            m.f.(buff)(&amp;m)\n        case swap:\n            m.f.(swap)(&amp;m, m.defense)\n        default:\n            fmt.Printf(\"%T\", m.f)\n        }\n        fmt.Println(m)\n    }\n}<\/pre>\n<p>\u6211\u4e0d\u592a\u64c5\u957f\u9ad8\u6548\u4ee3\u7801\uff0c\u800c\u4e14\u6211\u5b8c\u5168\u7406\u89e3\u6211\u53ef\u80fd\u4f1a\u5728\u8fd9\u91cc\u63d0\u524d\u4f18\u5316\uff1b\u7136\u800c\uff0c\u6211\u9700\u8981\u5bf9\u6570\u767e\u4e2a\u8fd9\u6837\u7684\u5361\u8fdb\u884c\u7f16\u7a0b\uff0c\u5e76\u4e14\u5982\u679c\u8fd9\u4e9b\u53ef\u8c03\u7528\u5bf9\u8c61\u5b58\u5728\u4e8e\u5168\u5c40\u8303\u56f4\u5185\u5e76\u4e14\u4e25\u91cd\u4f9d\u8d56\u7c7b\u578b\u65ad\u8a00\u4f1a\u4f7f\u7a0b\u5e8f\u53d8\u6162\uff0c\u90a3\u4e48\u6211\u5c06\u5728\u91cd\u65b0\u7ec4\u7ec7\u4ee3\u7801\u65f6\u9047\u5230\u9ebb\u70e6\u3002 <\/p>\n<p>\u60a8\u8ba4\u4e3a\u6211\u7684\u65b9\u6cd5\u6709\u4ec0\u4e48\u660e\u663e\u7684\u95ee\u9898\u5417\uff1f\u6211\u662f\u5426\u6b63\u786e\u5730\u5904\u7406\u4e86\u4e00\u6d41\u7684\u529f\u80fd\uff0c\u6216\u8005\u662f\u5426\u5b58\u5728\u67d0\u79cd\u6211\u770b\u4e0d\u5230\u7684\u660e\u663e\u7684\u6027\u80fd\u95ee\u9898\uff1f\u4efb\u4f55\u548c\u6240\u6709\u7684\u5e2e\u52a9\u5c06\u4e0d\u80dc\u611f\u6fc0\uff01 <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u4f7f\u7528\u666e\u901a\u51fd\u6570\u548c\u95ed\u5305\uff1a<\/p>\n<pre>type card struct {\n    name string\n    text string\n    fn   func(*monstercard)\n}\n\ntype monstercard struct {\n    card\n    attack  int\n    defense int\n}\n\nfunc (mc *monstercard) call() {\n    mc.fn(mc)\n}\n\nfunc increaseattack(mc *monstercard) {\n    mc.attack += 100\n}\n\nfunc switchstats(mc *monstercard) {\n    mc.attack, mc.defense = mc.defense, mc.attack\n}\n\n\nfunc updatetextandattack(text string, attack int) func(mc *monstercard) {\n    return func(mc *monstercard) {\n        mc.text, mc.attack = text, attack\n    }\n}<\/pre>\n<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u4f7f\u7528\u666e\u901a\u65b9\u6cd5\u548c\u95ed\u5305\uff1a<\/p>\n<pre>type card struct {\n    name string\n    text string\n}\n\ntype monstercard struct {\n    card\n    attack  int\n    defense int\n}\n\nfunc (mc *monstercard) increaseattack() {\n    mc.attack += 100\n}\n\nfunc (mc *monstercard) switchstats() {\n    mc.attack, mc.defense = mc.defense, mc.attack\n}\n\nfunc (mc *monstercard) updatetextandattack(text string, attack int) func() {\n    return func() {\n        mc.text, mc.attack = text, attack\n    }\n}<\/pre>\n<\/p>\n<p>\u611f\u8c22\u6240\u6709\u5e2e\u52a9\u6211\u627e\u5230\u7b54\u6848\u7684\u4eba\u3002\u770b\u6765\u6211\u4ecd\u5728\u4f7f\u7528\u6211\u7684\u811a\u672c\u8bed\u8a00\u601d\u7ef4\u6765\u5c1d\u8bd5\u7528\u7f16\u8bd1\u8bed\u8a00\u89e3\u51b3\u95ee\u9898\u3002\u6211\u4e0d\u9700\u8981\u5c1d\u8bd5\u521b\u5efa\u6700\u7ec8\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u7684\u63a5\u53e3\uff0c\u800c\u662f\u9700\u8981\u66f4\u52a0\u4e25\u683c\u5730\u81ea\u5b9a\u4e49\u7c7b\u578b\u5b9a\u4e49\uff0c\u5e76\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u5361\u7247\u6548\u679c\u5b9e\u73b0\u4e0d\u540c\u7684\u51fd\u6570\u7c7b\u578b\u3002\u5982\u6b64\u4e25\u683c\u7684\u6267\u884c\u4e5f\u5c06\u5141\u8bb8\u6e38\u620f\u4e2d\u5b58\u5728\u5177\u6709\u591a\u79cd\u6548\u679c\u7684\u5361\u724c\u3002 <\/p>\n<pre>package main\n\ntype card struct {\n    name string\n    text string\n}\n\ntype monsterCard struct {\n    card\n    attack int\n    defense int\n}\n\ntype buffEffect struct {\n    monsterCard\n    fn buff\n}\n\ntype setValueEffect struct {\n    monsterCard\n    fn swap\n}\n\ntype buff func(target *monsterCard)\n\ntype swap func(target *monsterCard, value int)\n\nvar increaseAttack buff = func(target *monsterCard)  {\n    target.attack += 100\n}\n\nvar setAttackValue swap = func(target *monsterCard, value int) {\n    target.attack = value\n}\n\nfunc main()  {\n    m1 := buffEffect{\n        monsterCard: monsterCard{\n            card:    card{\n                name: \"Celtic Guardian\",\n            },\n            attack:  1400,\n            defense: 1200,\n        },\n        fn:          increaseAttack,\n    }\n    m2 := setValueEffect{\n        monsterCard: monsterCard{\n            card:    card{\n                name: \"Dark Magician\",\n            },\n            attack:  2500,\n            defense: 2100,\n        },\n        fn:          setAttackValue,\n    }\n    m1.fn(&amp;m1.monsterCard)\n    m2.fn(&amp;m2.monsterCard, 100)\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aGo\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Go\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5 Go\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5 \u6765\u6e90\uff1astackoverflow 2024-04-29 21:18:36 0\u6d4f\u89c8 \u6536\u85cf Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aGo\u3001\u4e00\u6d41\u7684\u529f\u80fd\u548c\u6700\u4f73\u5b9e\u8df5\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u95ee\u9898\u5185\u5bb9 \u5728\u8fc7\u53bb\u7684\u51e0\u5929\u91cc\uff0c\u6211\u4e00\u76f4\u5728\u5bfb\u627e\u89e3\u51b3\u4e00\u6d41\u51fd\u6570\u95ee\u9898\u7684\u6700\u4f73\u65b9\u6cd5\uff08\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u5206\u914d\u7ed9\u67d0\u4e2a\u53d8\u91cf\uff09\u4ee5\u53ca\u6548\u7387\u65b9\u9762\u7684\u6700\u4f73\u5b9e\u8df5\u3002 \u5047\u8bbe\u6211\u6b63\u5728\u7f16\u5199 yugioh \u7eb8\u724c\u6e38\u620f\uff0c\u5e76\u4e14\u6211\u5e0c\u671b\u6bcf\u5f20\u7eb8\u724c\u7c7b\u578b\u7684\u7eb8\u724c\u81f3\u5c11\u5177\u6709\u4ee5\u4e0b\u5c5e\u6027\uff1a type card struct { name string text string } \u6211\u4e00\u76f4\u5728\u601d\u8003\u5728\u54ea\u91cc\uff08\u4ee5\u53ca\u5982\u4f55\uff09\u5bf9\u6bcf\u5f20\u5361\u7684\u5355\u72ec\u529f\u80fd\u8fdb\u884c\u7f16\u7a0b\u7684\u60f3\u6cd5\u3002\u6211\u76ee\u524d\u786e\u4fe1\uff0c\u7b2c\u4e00\u7c7b\u51fd\u6570\u7684\u6700\u4f73\u4f4d\u7f6e\u662f\u5728 type \u5361 struct \u4e2d\uff0c\u5e76\u50cf\u5728 python \u4e2d\u4e00\u6837\u5c06\u65b0\u5c5e\u6027\u521b\u5efa\u4e3a\u201c\u53ef\u8c03\u7528\u201d\uff08\u8f6c\u5230\u6f14\u793a\u94fe\u63a5\uff09\u3002 package main import &#8220;fmt&#8221; type card struct { name string text string f interface{} } type monsterCard struct { card attack [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43134","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43134"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43134\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}