{"id":1697,"date":"2023-06-27T13:54:14","date_gmt":"2023-06-27T05:54:14","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=1697"},"modified":"2023-06-27T13:54:14","modified_gmt":"2023-06-27T05:54:14","slug":"golang%e9%9d%a2%e8%af%95%e5%ae%9d%e5%85%b8-go%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b023%e7%a7%8d%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b9%8b%e8%a1%8c%e4%b8%ba%e5%9e%8b%e6%a8%a1%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/go\/golang%e9%9d%a2%e8%af%95%e5%ae%9d%e5%85%b8-go%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b023%e7%a7%8d%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b9%8b%e8%a1%8c%e4%b8%ba%e5%9e%8b%e6%a8%a1%e5%bc%8f-2\/","title":{"rendered":"Golang\u9762\u8bd5\u5b9d\u5178\u2014\u2014Go\u8bed\u8a00\u5b9e\u73b023\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u884c\u4e3a\u578b\u6a21\u5f0f(\u4e2d)"},"content":{"rendered":"<div class='wxsyncmain'>\n<section data-tool=\"mdnice\u7f16\u8f91\u5668\" data-website=\"https:\/\/www.mdnice.com\" style=\"color: black;padding-right: 10px;padding-left: 10px;line-height: 1.6;letter-spacing: 0px;word-break: break-word;overflow-wrap: break-word;text-align: left;font-size: 14px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;\">\n<h1 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(110, 150, 124);text-align: center;font-weight: bold;margin: 0.5em;\"><span style=\"padding-right: 1em;\">\/ <\/span><span style=\"display: none;\"><\/span>\u5173\u4e8eGolang\u9762\u8bd5\u5b9d\u5178<span style=\"padding-left: 1em;\">\/<\/span><\/h1>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;text-align: justify;\">\u6700\u8fd1\u51e0\u5e74\uff0cGo\u7684\u70ed\u5ea6\u6301\u7eed\u98d9\u5347\uff0c\u56fd\u5185\u5916\u5f88\u591a\u5927\u516c\u53f8\u90fd\u5728\u5927\u89c4\u6a21\u7684\u4f7f\u7528Go\u3002Google\u662fGo\u8bed\u8a00\u8bde\u751f\u7684\u5730\u65b9\uff0c\u5176\u4ed6\u516c\u53f8\u5982Facebook\u3001\u817e\u8baf\u3001\u963f\u91cc\u3001\u5b57\u8282\u8df3\u52a8\u3001\u767e\u5ea6\u3001\u4eac\u4e1c\u3001\u5c0f\u7c73\u7b49\u90fd\u5728\u62e5\u62b1\u548c\u8f6c\u5411Go\u3002Go\u8bed\u8a00\u7684\u5f00\u6e90\u9879\u76ee\u4e5f\u975e\u5e38\u591a\uff0c\u5982kubernetes\u3001docker\u3001etcd\u3002<\/p>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;text-align: justify;\">\u968f\u7740\u5e02\u573a\u5bf9Go\u8bed\u8a00\u4eba\u624d\u9700\u6c42\u7684\u589e\u957f\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u6295\u5165\u4e86Go\u8bed\u8a00\u7684\u6000\u62b1\u3002\u672c\u7cfb\u5217\u6587\u7ae0\u5c06\u4ee5\u7b2c\u4e00\u89c6\u89d2\u4e0e\u5927\u5bb6\u4e00\u540c\u5f00\u59cbGolang\u7684\u9762\u8bd5\u4e4b\u8def\uff0c\u5e0c\u671b\u5927\u5bb6\u80fd\u591f\u6709\u6240\u6536\u83b7\uff0c\u62ff\u4e0b\u5fc3\u4eea\u7684offer\u3002<\/p>\n<h1 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(110, 150, 124);text-align: center;font-weight: bold;margin: 0.5em;\"><span style=\"padding-right: 1em;\">\/ <\/span><span style=\"display: none;\"><\/span>\u4f7f\u7528Go\u5b9e\u73b023\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u2014\u2014\u884c\u4e3a\u578b\u6a21\u5f0f\uff08\u4e2d\uff09<span style=\"padding-left: 1em;\">\/<\/span><\/h1>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgba(0, 0, 0, 0.6);font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\"><span style=\"display: none;\"><\/span>\u89c2\u5bdf\u8005\u6a21\u5f0f<\/h2>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;text-align: justify;\">\u5b9a\u4e49\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6240\u6709\u4f9d\u8d56\u4e8e\u5b83\u7684\u5bf9\u8c61\u90fd\u5f97\u5230\u901a\u77e5\u5e76\u88ab\u81ea\u52a8\u66f4\u65b0<\/p>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u9002\u7528\u573a\u666f<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5bf9\u4e00\u4e2a\u5bf9\u8c61\u72b6\u6001\u6216\u6570\u636e\u7684\u66f4\u65b0\u9700\u8981\u5176\u4ed6\u5bf9\u8c61\u540c\u6b65\u66f4\u65b0\uff0c\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u7684\u66f4\u65b0\u9700\u8981\u4f9d\u8d56\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u66f4\u65b0<\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5bf9\u8c61\u4ec5\u9700\u8981\u5c06\u81ea\u5df1\u7684\u66f4\u65b0\u901a\u77e5\u7ed9\u5176\u4ed6\u5bf9\u8c61\u800c\u4e0d\u9700\u8981\u77e5\u9053\u5176\u4ed6\u5bf9\u8c61\u7684\u7ec6\u8282\uff0c\u5982\u6d88\u606f\u63a8\u9001\u7b49<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>Go\u8bed\u8a00\u5b9e\u73b0<span style=\"display: none;\"><\/span><\/h3>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;color: #abb2bf;background: #282c34;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;border-radius: 6px;padding: 1em;\"><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Event&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;Data&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span><br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;IObServer&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">interface<\/span>&nbsp;{<br  \/>&nbsp;Update(event&nbsp;*Event)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;ISubject&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">interface<\/span>&nbsp;{<br  \/>&nbsp;Register(server&nbsp;IObServer)<br  \/>&nbsp;Remove(server&nbsp;IObServer)<br  \/>&nbsp;Notify(event&nbsp;*Event)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;ObServer&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;Id&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">int<\/span><br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*ObServer)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">Update<\/span><span style=\"line-height: 26px;\">(event&nbsp;*Event)<\/span><\/span>&nbsp;{<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"observer&nbsp;%d&nbsp;msg:&nbsp;%s\"<\/span>,&nbsp;s.Id,&nbsp;event.Data)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Subject&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;ObServers&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">map<\/span>[IObServer]<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>{}<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">NewSubject<\/span><span style=\"line-height: 26px;\">()<\/span>&nbsp;*<span style=\"color: #61aeee;line-height: 26px;\">Subject<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">return<\/span>&nbsp;&amp;Subject{ObServers:&nbsp;<span style=\"color: #e6c07b;line-height: 26px;\">make<\/span>(<span style=\"color: #c678dd;line-height: 26px;\">map<\/span>[IObServer]<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>{})}<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*Subject)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">Register<\/span><span style=\"line-height: 26px;\">(server&nbsp;IObServer)<\/span><\/span>&nbsp;{<br  \/>&nbsp;s.ObServers[server]&nbsp;=&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>{}{}<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*Subject)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">Remove<\/span><span style=\"line-height: 26px;\">(server&nbsp;IObServer)<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #e6c07b;line-height: 26px;\">delete<\/span>(s.ObServers,&nbsp;server)<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*Subject)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">Notify<\/span><span style=\"line-height: 26px;\">(event&nbsp;*Event)<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">for<\/span>&nbsp;ob,&nbsp;_&nbsp;:=&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">range<\/span>&nbsp;s.ObServers&nbsp;{<br  \/>&nbsp;&nbsp;ob.Update(event)<br  \/>&nbsp;}<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">main<\/span><span style=\"line-height: 26px;\">()<\/span><\/span>&nbsp;{<br  \/>&nbsp;s&nbsp;:=&nbsp;NewSubject()<br  \/><br  \/>&nbsp;o1&nbsp;:=&nbsp;ObServer{Id:&nbsp;<span style=\"color: #d19a66;line-height: 26px;\">1<\/span>}<br  \/>&nbsp;o2&nbsp;:=&nbsp;ObServer{Id:&nbsp;<span style=\"color: #d19a66;line-height: 26px;\">2<\/span>}<br  \/><br  \/>&nbsp;s.Register(&amp;o1)<br  \/>&nbsp;s.Register(&amp;o2)<br  \/><br  \/>&nbsp;s.Notify(&amp;Event{Data:&nbsp;<span style=\"color: #98c379;line-height: 26px;\">\"123\"<\/span>})<br  \/>}<br  \/><\/code><\/pre>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u89c2\u5bdf\u8005\u6a21\u5f0f\u4f18\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u4f60\u65e0\u9700\u4fee\u6539\u88ab\u89c2\u5bdf\u8005\u4ee3\u7801\u5c31\u80fd\u5f15\u5165\u65b0\u7684\u89c2\u5bdf\u8005<\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5efa\u7acb\u5bf9\u8c61\u4e4b\u95f4\u7684\u8054\u7cfb<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u89c2\u5bdf\u8005\u6a21\u5f0f\u7f3a\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u89c2\u5bdf\u8005\u7684\u901a\u77e5\u987a\u5e8f\u662f\u968f\u673a\u7684<\/section>\n<\/li>\n<\/ol>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgba(0, 0, 0, 0.6);font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\"><span style=\"display: none;\"><\/span>\u5907\u5fd8\u5f55\u6a21\u5f0f<\/h2>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;text-align: justify;\">\u5728\u4e0d\u7834\u574f\u5c01\u88c5\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u6355\u83b7\u4e00\u4e2a\u5bf9\u8c61\u7684\u5185\u90e8\u72b6\u6001\uff0c\u5e76\u5728\u8be5\u5bf9\u8c61\u4e4b\u5916\u4fdd\u5b58\u8fd9\u4e2a\u72b6\u6001<\/p>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u9002\u7528\u573a\u666f<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u56de\u9000\u573a\u666f\u3001\u64a4\u9500\u91cd\u505a<\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5907\u4efd\u4e0e\u8fd8\u539f\u3001\u5feb\u7167\u4e0e\u6062\u590d<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>Go\u8bed\u8a00\u5b9e\u73b0<span style=\"display: none;\"><\/span><\/h3>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;color: #abb2bf;background: #282c34;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;border-radius: 6px;padding: 1em;\"><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Memento&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;State&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span><br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(m&nbsp;*Memento)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">SetState<\/span><span style=\"line-height: 26px;\">(s&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;m.State&nbsp;=&nbsp;s<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(m&nbsp;*Memento)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">GetState<\/span><span style=\"line-height: 26px;\">()<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">string<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">return<\/span>&nbsp;m.State<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(m&nbsp;*Memento)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">CreateMemento<\/span><span style=\"line-height: 26px;\">()<\/span>&nbsp;*<span style=\"color: #61aeee;line-height: 26px;\">Memento<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">return<\/span>&nbsp;&amp;Memento{State:&nbsp;m.State}<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;CareTaker&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;memento&nbsp;*Memento<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(c&nbsp;*CareTaker)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">GetMemento<\/span><span style=\"line-height: 26px;\">()<\/span>&nbsp;*<span style=\"color: #61aeee;line-height: 26px;\">Memento<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">return<\/span>&nbsp;c.memento<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(c&nbsp;*CareTaker)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">SetMemento<\/span><span style=\"line-height: 26px;\">(m&nbsp;*Memento)<\/span><\/span>&nbsp;{<br  \/>&nbsp;c.memento&nbsp;=&nbsp;m<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">main<\/span><span style=\"line-height: 26px;\">()<\/span><\/span>&nbsp;{<br  \/>&nbsp;o&nbsp;:=&nbsp;Memento{State:&nbsp;<span style=\"color: #98c379;line-height: 26px;\">\"running\"<\/span>}<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"\u5f53\u524d\u72b6\u6001:&nbsp;%s\"<\/span>,&nbsp;o.State)<br  \/><br  \/>&nbsp;c&nbsp;:=&nbsp;<span style=\"color: #e6c07b;line-height: 26px;\">new<\/span>(CareTaker)<br  \/>&nbsp;c.SetMemento(o.CreateMemento())<br  \/><br  \/>&nbsp;o.SetState(<span style=\"color: #98c379;line-height: 26px;\">\"finish\"<\/span>)<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"\u4fee\u6539\u540e\u7684\u72b6\u6001:&nbsp;%s\"<\/span>,&nbsp;o.State)<br  \/><br  \/>&nbsp;o.SetState(c.GetMemento().GetState())<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"\u8fd8\u539f\u540e\u7684\u72b6\u6001:&nbsp;%s\"<\/span>,&nbsp;o.State)<br  \/>}<br  \/><\/code><\/pre>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u5907\u5fd8\u5f55\u6a21\u5f0f\u4f18\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u80fd\u591f\u5f88\u65b9\u4fbf\u7684\u56de\u5230\u67d0\u4e2a\u5386\u53f2\u72b6\u6001<\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5b9e\u73b0\u4e86\u4fe1\u606f\u7684\u5c01\u88c5\uff0c\u4f7f\u7528\u65b9\u4e0d\u9700\u8981\u5173\u5fc3\u72b6\u6001\u7684\u4fdd\u5b58\u7ec6\u8282<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u5907\u5fd8\u5f55\u6a21\u5f0f\u7f3a\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u6d88\u8017\u8d44\u6e90<\/section>\n<\/li>\n<\/ol>\n<h2 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgba(0, 0, 0, 0.6);font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\"><span style=\"display: none;\"><\/span>\u4e2d\u4ecb\u8005\u6a21\u5f0f<\/h2>\n<p data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"font-size: 16px;padding-top: 8px;padding-bottom: 8px;line-height: 26px;text-align: justify;\">\u5b9a\u4e49\u4e00\u4e2a\u4e2d\u4ecb\u5bf9\u8c61\u6765\u7b80\u5316\u539f\u6709\u5bf9\u8c61\u4e4b\u95f4\u7684\u4ea4\u4e92\u5173\u7cfb\uff0c\u964d\u4f4e\u7cfb\u7edf\u4e2d\u5bf9\u8c61\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u4f7f\u539f\u6709\u5bf9\u8c61\u4e4b\u95f4\u4e0d\u5fc5\u76f8\u4e92\u4e86\u89e3<\/p>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u9002\u7528\u573a\u666f<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u7cfb\u7edf\u4e2d\u7684\u5bf9\u8c61\u4e4b\u95f4\u5b58\u5728\u590d\u6742\u7684\u5f15\u7528\u5173\u7cfb\uff0c\u5982\u76f8\u4e92\u4f9d\u8d56<\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5b58\u5728\u4ea4\u4e92\u7684\u516c\u5171\u884c\u4e3a\uff0c\u5982\u679c\u9700\u8981\u6539\u53d8\u884c\u4e3a\u53ef\u589e\u52a0\u4e2d\u4ecb\u8005\u7c7b<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>Go\u8bed\u8a00\u5b9e\u73b0<span style=\"display: none;\"><\/span><\/h3>\n<pre data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px;margin-bottom: 10px;\"><code style=\"overflow-x: auto;color: #abb2bf;background: #282c34;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;border-radius: 6px;padding: 1em;\"><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Item&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">interface<\/span>&nbsp;{<br  \/>&nbsp;SendMsg(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<br  \/>&nbsp;GetMsg(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;User&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;mediator&nbsp;*Mediator<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(u&nbsp;*User)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">SendMsg<\/span><span style=\"line-height: 26px;\">(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;u.mediator.ForwardMsg(u,&nbsp;msg)<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(u&nbsp;*User)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">GetMsg<\/span><span style=\"line-height: 26px;\">(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"user&nbsp;get&nbsp;msg:&nbsp;%s:&nbsp;\"<\/span>,&nbsp;msg)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Server&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;mediator&nbsp;*Mediator<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*Server)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">SendMsg<\/span><span style=\"line-height: 26px;\">(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;s.mediator.ForwardMsg(s,&nbsp;msg)<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(s&nbsp;*Server)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">GetMsg<\/span><span style=\"line-height: 26px;\">(msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"server&nbsp;get&nbsp;msg:&nbsp;%s\"<\/span>,&nbsp;msg)<br  \/>}<br  \/><br  \/><span style=\"color: #c678dd;line-height: 26px;\">type<\/span>&nbsp;Mediator&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">struct<\/span>&nbsp;{<br  \/>&nbsp;User<br  \/>&nbsp;Server<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"line-height: 26px;\">(m&nbsp;*Mediator)<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">ForwardMsg<\/span><span style=\"line-height: 26px;\">(item&nbsp;Item,&nbsp;msg&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">string<\/span>)<\/span><\/span>&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">switch<\/span>&nbsp;item.(<span style=\"color: #c678dd;line-height: 26px;\">type<\/span>)&nbsp;{<br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">case<\/span>&nbsp;*User:<br  \/>&nbsp;&nbsp;m.Server.GetMsg(msg)<br  \/>&nbsp;&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">break<\/span><br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">case<\/span>&nbsp;*Server:<br  \/>&nbsp;&nbsp;m.User.GetMsg(msg)<br  \/>&nbsp;&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">break<\/span><br  \/>&nbsp;<span style=\"color: #c678dd;line-height: 26px;\">default<\/span>:<br  \/>&nbsp;&nbsp;fmt.Printf(<span style=\"color: #98c379;line-height: 26px;\">\"Error\"<\/span>)<br  \/>&nbsp;}<br  \/>}<br  \/><br  \/><span style=\"line-height: 26px;\"><span style=\"color: #c678dd;line-height: 26px;\">func<\/span>&nbsp;<span style=\"color: #61aeee;line-height: 26px;\">main<\/span><span style=\"line-height: 26px;\">()<\/span><\/span>&nbsp;{<br  \/>&nbsp;mediator&nbsp;:=&nbsp;Mediator{}<br  \/><br  \/>&nbsp;u&nbsp;:=&nbsp;User{}<br  \/>&nbsp;s&nbsp;:=&nbsp;Server{}<br  \/><br  \/>&nbsp;mediator.User&nbsp;=&nbsp;u<br  \/>&nbsp;mediator.Server&nbsp;=&nbsp;s<br  \/><br  \/>&nbsp;u.SendMsg(<span style=\"color: #98c379;line-height: 26px;\">\"Hello\"<\/span>)<br  \/>&nbsp;s.SendMsg(<span style=\"color: #98c379;line-height: 26px;\">\"Hi\"<\/span>)<br  \/>}<br  \/><\/code><\/pre>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u4e2d\u4ecb\u8005\u6a21\u5f0f\u4f18\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u51cf\u5c11\u7c7b\u95f4\u7684\u4f9d\u8d56\uff0c\u964d\u4f4e\u7c7b\u95f4\u8026\u5408<\/section>\n<\/li>\n<\/ol>\n<h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"color: rgb(90, 120, 131);font-size: 14px;font-weight: bold;margin-top: 0.5em;margin-bottom: 0.5em;\">? <span style=\"display: none;\"><\/span>\u4e2d\u4ecb\u8005\u6a21\u5f0f\u7f3a\u70b9<span style=\"display: none;\"><\/span><\/h3>\n<ol data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 8px;margin-bottom: 8px;padding-left: 25px;\" class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);\">\u5f53\u540c\u4e8b\u7c7b\u8d8a\u591a\u65f6\u4e2d\u4ecb\u8005\u5c31\u8d8a\u81c3\u80bf<\/section>\n<\/li>\n<\/ol>\n<\/section>\n<p><br  \/><\/p>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u8349\u8393\u6a59\u6c41\u513f              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\/ \u5173\u4e8eGolang\u9762\u8bd5\u5b9d\u5178\/ \u6700\u8fd1\u51e0\u5e74\uff0cGo\u7684\u70ed\u5ea6\u6301\u7eed\u98d9\u5347\uff0c\u56fd\u5185\u5916\u5f88\u591a\u5927\u516c\u53f8\u90fd\u5728\u5927\u89c4\u6a21\u7684\u4f7f\u7528Go\u3002Google\u662fGo\u8bed\u8a00\u8bde\u751f\u7684\u5730 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":1696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[23],"tags":[],"class_list":["post-1697","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212-1024x435.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230528044536212.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Go","post_date":"2023-06-27","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":274,"next_post_id":1670,"next_post_title":"Golang\u9762\u8bd5\u5b9d\u5178\u2014\u2014Go\u8bed\u8a00\u5b9e\u73b023\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u7ed3\u6784\u578b\u6a21\u5f0f(\u4e0b)","previous_post_id":null,"previous_post_title":null,"_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1697"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1697\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/1696"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}