﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-战魂小筑</title><link>http://www.cppblog.com/sunicdavy/</link><description>讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu</description><language>zh-cn</language><lastBuildDate>Thu, 16 Apr 2026 09:35:34 GMT</lastBuildDate><pubDate>Thu, 16 Apr 2026 09:35:34 GMT</pubDate><ttl>60</ttl><item><title>Entity-Component-System框架心得</title><link>http://www.cppblog.com/sunicdavy/archive/2018/11/30/216103.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Fri, 30 Nov 2018 10:01:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2018/11/30/216103.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/216103.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2018/11/30/216103.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/216103.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/216103.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 1em; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">近期在游戏demo中试验ECS， 深入研究Unity官方的ECS框架和第三方Entitas框架， 分享下使用ECS的心得。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">Unity在2018版中加入了ECS系统， 但处于小白鼠阶段。默认不是Unity的一部分， 需要手动下载代码并导入Packages（新特性）。官方提供海豚例子， 但除此之外例子和资料非常少。所以完全无法，也不敢在demo中贸然引入这种系统，所以放弃官方ECS系统。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">第三方的Entitas（<span style="border-bottom: 1px dashed gray;"><a href="http://link.zhihu.com/?target=https%3A//github.com/sschmid/Entitas-CSharp"  external"="" target="_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043" style="text-decoration-line: none; cursor: pointer; border-bottom: 1px solid gray;"><span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 0px; line-height: 0; font-family: a; color: transparent; text-shadow: none; background-color: transparent;">https://</span>github.com/sschmid/Enti<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 0px; line-height: 0; font-family: a; color: transparent; text-shadow: none; background-color: transparent;">tas-CSharp</span></a></span>）ECS框架从2015年就开始在各地演讲中介绍。整体框架基于代码生成， 能解决一部分的代码爆炸问题， 而且性能也要好一些。例子，介绍非常丰富，例子虽然基于不同版本的Entitas，特性支持和最新版差不太多， 只是写法有细微差异， 对于理解来说无碍。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">经过1~2天的改造， 终于将demo从传统Unity写法改造为ECS标准写法，新增了46个文件， 而传统逻辑一共只有16个文件，大概对比下ECS的特点和差异。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">Entitas的ECS系统</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">1. 本来在一个对象中添加一个类字段的过程，ECS需要添加一个类代表Component，并且代码生成。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">这个字段一般用于描述对象的资源，处理显示的GameObject， 表示对象的类型等。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">2. 本来一个对象的业务逻辑处理过程直接用方法解决的， ECS需要新加一个System，而操作对象需要使用Filter或Group查询获得。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">3. 一系列的操作， 需要拆分为多个System和Component拆分处理。如果System顺序不对， 会造成一些诡异的bug。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">4. Component不仅仅是Model承载体， 也可以是参数的数据结构。参数Component通过Entity传递到System处理。 例如： 通过ECS创建一个方块的过程，使用CreateTileComponent，包含创建Tile的位置， 创建Entity并添加CreateTileComponent， 在CreateTileSystem中处理就创建了Tile，处理完成时， 需要将传入的Entity.Destroy掉。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">6. Entity上修改Component的过程， 会触发事件。修改的过程需要使用RelaceXXX，XXX表示组件名。组件可以频繁修改， 不用担心添加和删除组件过程的性能， Entitas底层处理性能只相当于指针赋值的性能。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">ECS像什么？</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">1. ECS中的System类似触发器系统（Event-Condition-Action），其中，Event对应Entitas的GetTrigger+Collector，表示触发事件。Condition对应Filter表示在事件来源对象中找到需要的对象。 Action对应Execute，表示实际的操作。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">2. ECS中的Component类似不用lua扩展的Redis或者不用存储过程的MySQL， 纯粹纯数据， 而不能对数据有任何封装操作。没有lua和存储过程支持的db写起来还是比较费劲的，但ECS就是那么的纯。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">3. ECS中的Entity很尴尬，因为Component是按类别连续存储的以保证性能。 逻辑又需要Entity组合成逻辑需要的复合对象。 两边都要照顾，所以这种设计就让代码量巨增，可读性下降。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">ECS企图用一套框架灭掉设计模式</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">1. 单件（Singleton）在Entitas用Unique标签标记Component， 在Context中就是唯一的， 其实也就是Singleton。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">2. ECS干掉了传统的工厂模式，底层统一对对象（Entity）和属性（Component）统一管理。需要按Component中的值找回Entity时， 可以使用EntityIndex。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">3. Entity携带不同的组件时，整个创建和销毁过程被记录并恢复，其实就是Command模式</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">ECS适合做UI框架（类似MVVM，MVC,MVP）么？</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">ECS不是专用的UI框架，但是可以对不同系统和数据间解耦。传统代码中数据修改后的Callback，ECS也可以用Listener做， 但Listener因为能保存数据， 就需要用Component保存。 所以你需要面对的是，一个Button，响应创建一个参数用的Component和System，还要为数据改变写一套ListenComponent和Listener处理的System，酸爽吧？</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">Minecraft适合ECS来做么？</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">可以，性能应该能提升不少，但是代码会更繁琐，特别像Java这种啰嗦语言配上ECS这种啰嗦框架，估计代码量翻翻还是很轻松的。MC属于特殊类型的游戏，适合特殊领域特别优化，也就是专门为方块做出特别的设计来做优化。ECS属于通用框架，即便性能OK，但是代码未必能有良好的可读性。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">体量小的游戏适合用ECS来做么？</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">可以，但不建议。特别是只有几个人维护的工程，贸然上ECS系统，会让系统变的极为复杂。当然你会说，如果开发到后期，传统开发模式会导致代码会乱，ECS会好些吧。掌握ECS也不是一天两天的事情，不熟悉ECS的程序员设计出来的系统获得的优势可能还不如用传统设计方法好呢。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">架构解决的是人的问题， 人都有问题，用什么框架都没办法。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">到底什么项目适合用ECS？</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">1. 大量的小个体不断的生成和销毁以及显示，例如： 攻城战中，要体现每个角色的移动，战斗。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">2. 多于5个人编写核心战斗逻辑。互相协作和模块切分，需要一个大家都能信服的框架，ECS可以选择。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">P.S.</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">不要造ECS的轮子！</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">很多同学看了ECS基本原理，在没有深入使用过任何ECS系统时马上操刀造轮子。ECS系统确实看起来简单。实际造下来你会发现，性能非常糟糕以及不知道一些逻辑如何用ECS来解决。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><span style="font-weight: 600;">总结：</span></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">1. ECS确实为性能而生，没有并发加持性能的ECS都是耍流氓，要快就要快到极致。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">2. Unity中，ECS并发能扩展CPU的利用率，但是GPU的性能依然还是DrawCall优化那一套，别期望ECS会颠覆Unity，性能也不会快到飞起，关键还是要看具体的项目和人。</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">3. ECS是万能框架，但不全能。传统架构和设计思想也不是一无是处，熟啥用啥，怎么快怎么来！<br /><br /></p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;">无耻的广告链接，请各位支持</p><p style="margin: 1em 0px; color: #1a1a1a; font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: medium; background-color: #ffffff;"><a href="http://link.zhihu.com/?target=https%3A//item.jd.com/12380444.html" wrap=""  external"="" target="_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043" style="text-decoration-line: none; cursor: pointer; border-bottom: 1px solid gray;">《Go语言从入门到进阶实战（视频教学版）》(徐波)【摘要 书评 试读】- 京东图书</a></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/216103.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2018-11-30 18:01 <a href="http://www.cppblog.com/sunicdavy/archive/2018/11/30/216103.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>游戏与Web的服务器对比</title><link>http://www.cppblog.com/sunicdavy/archive/2018/08/29/215887.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 29 Aug 2018 03:16:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2018/08/29/215887.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/215887.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2018/08/29/215887.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/215887.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/215887.html</trackback:ping><description><![CDATA[<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">为了编写基于</span><a href="https://github.com/davyxu/cellnet"><span style="font-size: 16px; color: #003884;">cellnet</span></a><span style="font-size: 16px; color: #333333;">的新一代游戏服务器框架，最近深入研究微服务，ServiceMesh等概念。研究过程中对Web和游戏两种服务器架构设计有一些心得，编写并记录下来。(下文中，Game表示游戏服务器，Web表示Web服务器) `` </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">状态缓存</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">所谓状态缓存，就是在内存而非专业数据缓存服务器（如redis）中保存和处理逻辑数据，手动编写此过程较为繁琐但是效率较高，但随着状态逻辑复杂性和并发、扩容问题提出，状态同步会变得越来越复杂。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">强交互性的服务器类型需要在服务器做缓存，逻辑编写也较为容易，无需处理事务并发问题。例如：组队，匹配，战斗逻辑。服务器不能随意重启。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">弱交互性的服务器类型可配合redis做成无状态服务器，例如：养成，技能升级，领取物品等。服务器随时支持重启。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏服务器为了提高性能，早期所有服务器都是使用状态缓存写法编写，特别是MMORPG这类强交互的游戏服务器尤为严重。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">均为无状态服务器，弱交互。使用事务方式处理并发逻辑，例如：交易，下单等。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">推送，单独发送</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">这里提到的所谓推送，单独发送是与RPC区别的通讯方法。RPC要求请求必须有回应。而推送单独发送则更像是通知和广播，无需目的方返回任何消息。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">找到服务器的Session，直接Send</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">通过中转服务器，或称为中心服务器进行注册/广播</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">客户端的model数据需要更新时，服务器会主动推送消息。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏服务器没有严格的RPC设计需求，推送和单独发送较Web服务器更多。而且游戏服务器多使用长连接，所以主动推送也比Web服务器来的方便一些。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">将推送做成专有的服务，并做排队和并发处理。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">可用性</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">听说过游戏停服更新，支付宝服务器在刷二维码时停服了可一定被骂惨吧。Web对服务器高可用性要求很高，游戏虽然也注重服务器稳定性和可用性，但是由于版本迭代更新频繁，停服更新反而能获得玩家接受。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏对可用性要求不高。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏大版本更新时需要停服更新。支持热更新技术的服务器（例如Erlang，Skynet）仅使用热更新修复bug，很少直接更新新版本。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">不是所有的游戏服务器支持动态添加服务器。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">极高的可用性，服务不允许停服更新，使用蓝绿及灰度方式更新服务器。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">随时可以横向扩展服务器，提高服务器容量和承载。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">连接及传输</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">均使用TCP传输协议，游戏服务器注重性能，自有协议及二进制协议使用较多。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web注重兼容和接口友好，使用JSON格式较多。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">使用长连接，需要从逻辑层维护连接状态及处理服务器不在线情况</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">使用自有封包格式，大部分使用protobuf或二进制流格式。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">微服务大部分使用短连接，grpc支持http2长连接</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">使用json编码方便调试和版本兼容。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">流量限制</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">人数多了，任何服务器都扛不住，流量限制和登入限制能有效保护服务器稳定。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">单服有人数限制，可以通过GM后台设置挡墙，超过无法进入</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">限流器中间件，可以精确到服务控制流量</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">断流，防止雪崩</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏没有，也不需要这种概念，游戏请求不会突然升高，即便有，也通过GM后台人为控制</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">断流器中间件 </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">服务发现</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">如何找到服务器地址。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">服务有变化时，通过Watch系统通知订阅者更新本地缓存</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">服务器没有变化时，使用本地缓存找到服务地址</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">游戏服务器互相依赖复用只在很小的范围内，因此无需在不同语言不同进程服务间获得地址，大部分在配置文件中填写各服务的IP及地址即可互相访问。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">早期游戏自己编写服务器状态及地址发现服务。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">有用redis做服务发现</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">使用服务发现系统，分布式部署。无需依赖配置文件</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333; font-weight: bold;">网关需求</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Game:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">网关处理客户端上下线通知，心跳，维持连接，转发，广播上下行封包</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">Web:</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">根据请求地址路由，无上下线概念，无心跳。广播通过消息推送系统完成</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;"> </span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">由于笔者从事游戏行业，对Web服务器概念在逐渐熟悉中，若有错误和不足请各位大佬指出。</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #333333;">本人新书《Go语言从入门到进阶实战》，生动的语言，例子带有各种彩蛋，轻松了解Go语言特性，更有</span><a href="https://github.com/davyxu/cellnet"><span style="font-size: 16px; color: #003884;">cellnet</span></a><span style="font-size: 16px; color: #333333;">框架剖析解密</span></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div>
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><a href="https://search.jd.com/Search?keyword=go%E8%AF%AD%E8%A8%80%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9B%E9%98%B6%E5%AE%9E%E6%88%98&amp;enc=utf-8&amp;suggest=1.def.0.V02&amp;wq=Go%E8%AF%AD%E8%A8%80%E4%BB%8E&amp;pvid=145d55a92cab4b07b71326f8beb1700b"><span style="font-size: 16px; color: #003884;">https://search.jd.com/Search?keyword=go%E8%AF%AD%E8%A8%80%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9B%E9%98%B6%E5%AE%9E%E6%88%98&amp;enc=utf-8&amp;suggest=1.def.0.V02&amp;wq=Go%E8%AF%AD%E8%A8%80%E4%BB%8E&amp;pvid=145d55a92cab4b07b71326f8beb1700b</span></a></div>
<img src ="http://www.cppblog.com/sunicdavy/aggbug/215887.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2018-08-29 11:16 <a href="http://www.cppblog.com/sunicdavy/archive/2018/08/29/215887.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>嵌套git库的管理</title><link>http://www.cppblog.com/sunicdavy/archive/2017/12/19/215432.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Tue, 19 Dec 2017 14:04:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/12/19/215432.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/215432.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/12/19/215432.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/215432.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/215432.html</trackback:ping><description><![CDATA[<div yne-bulb-block="heading" yne-bulb-level="1" id="8683-1513690294872" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 28px; font-weight: bold;">嵌套git库的管理</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">使用git作为源代码管理时，经常需要在一个git代码库中从外网获取新的git库，也就是在git库下嵌套另外一个git库。而同时维护两个git库的过程就变的非常复杂。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="heading" yne-bulb-level="2" id="9569-1513690617117" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 20px; font-weight: bold;">submodule的弊端</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">常见的做法是使用git 提供的submodule功能。但submodule的管理嵌套git库的结果往往不是我们期望的结果。假设有一个git库叫project，在project的某个子目录下还包含一个叫3rd的目录，是另外一个git库。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">D:.</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">&#9492;&#9472;project</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">    &#9492;&#9472;3rd</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">            mytext.txt</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">假设甲和乙都取了project的代码，由于时间差异，甲通过submodule取到V1版，而乙取到了V2版本。甲乙同时在不同的3rd库下进行开发，势必会造成不同的运行结果。也许你认为可以在获取submodule时指定版本，但这个获取过程很难控制。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">一般说来，第三方库应由主程序进行更新及维护，一般情况下，项目没有特殊需求时，不会随便更新第三方库到最新版本，因此submodule更新嵌套的git库并不是理想的解决方案。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="heading" yne-bulb-level="2" id="3018-1513690681993" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 20px; font-weight: bold;">嵌套git库的修改可见性</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">前面的例子中，project git库下的3rd的git库中如果有文件发生修改，此时在project目录下，使用sourceTree等git管理工具无法识别3rd下的文件修改。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">也就是说，无法将3rd下的修改提交到project的git库中。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="heading" yne-bulb-level="2" id="2131-1513690883989" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 20px; font-weight: bold;">终极解决方案</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">此时，<span style="font-weight: bold;">将3rd目录下的.git目录暂时移出project和3rd目录</span>，此时，在project目录用sourceTree查看时，将可以看到3rd下的文件修改，提交修改到project库中。再将刚移出去的3rd的.git目录移回3rd目录下，在3rd目录下使用git status，将可看到3rd目录的修改，提交时，会将修改提交到3rd目录。</div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><img src ="http://www.cppblog.com/sunicdavy/aggbug/215432.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-12-19 22:04 <a href="http://www.cppblog.com/sunicdavy/archive/2017/12/19/215432.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在Windows下运行Linux Shell</title><link>http://www.cppblog.com/sunicdavy/archive/2017/11/27/215373.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Mon, 27 Nov 2017 07:15:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/11/27/215373.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/215373.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/11/27/215373.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/215373.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/215373.html</trackback:ping><description><![CDATA[<p data-source-line="2" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;"></p><h1></h1><p data-source-line="2" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">越来越多的人选择用Mac或者Linux环境进行跨平台项目开发。但是仍然有大部分人习惯于在Windows环境下进行开发，毕竟Windows在各方面使用还是较为方便，特别像文件版本管理（Git，SVN等）</p><p data-source-line="4" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">在跨平台下开发游戏或软件，就需要有一套方便的自动化工具。Windows下需要使用批处理，虽然有PowerShell加持，但这东西学了也不靠谱，只有一个平台能用。大家还是习惯Linux Shell。连Mac平台都可以用Shell，Windows上要做自动化脚本就显得非常尴尬。</p><p data-source-line="6" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">我曾经在项目中使用go编写了一套将配置转为批处理和Linux Shell的工具。使用过程较为复杂，但是能跨平台进行表格导出和协议编译等工作。</p><p data-source-line="8" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">但是，这个工具还是需要对不同的平台编写多套模板进行代码生成，非常繁琐。如果有一套跨平台的Shell，编写一次就可以跨平台运行那该多好。</p><p data-source-line="10" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">查阅资料后，一共有几个方案：</p><ol data-source-line="11" style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">使用Python作为自动化工具 这个方案其实就是使用python把批处理和Shell干的事情用代码来解决。但前提是要重新学习Python，也需要一部分熟悉简单的Python语法，人为学习成本较高，也比较费事。</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">自己编写一套独立的自动化工具 这个方案需要长时间的适配过程，差什么指令补什么指令，对项目进度有一定干扰。</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">自己编写Linux Shell的解释器 这个就更难了，要做到100%兼容，基本不可能。</p></li></ol><ol start="4" data-source-line="21" style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;"><li style="box-sizing: border-box;">使用Cygwin和Mingw 需要一个微型运行时进行Linux Shell的解释，msys大概是18M左右，可行性较高。</li></ol><p data-source-line="24" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">在研究Cygwin和Mingw如何整合的过程中，我误操作点击了一个sh后缀的Linux Shell，这是我希望让Mingw运行的Shell。结果呢，sh后缀的文件居然能在Windows下运行。我马上编写了一系列的例子，发现几乎完全兼容常用的Shell指令。 经过研究，我发现Windows下能运行sh文件是由Git自带的msys2提供的。MSYS2 （Minimal SYStem 2，&nbsp;<a href="http://www.msys2.org/%EF%BC%89" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;">http://www.msys2.org/）</a>&nbsp;是一个MSYS的独立改写版本，主要用于 shell 命令行开发环境。同时它也是一个在Cygwin （POSIX 兼容性层） 和 MinGW-w64（从"MinGW-生成"）基础上产生的，追求更好的互操作性的 Windows 软件。</p><p data-source-line="27" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff;">那就是说，<span style="box-sizing: border-box; font-weight: 600;">只要安装了Git（<a href="https://git-scm.com/%EF%BC%89%EF%BC%8C%E9%82%A3%E4%B9%88%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%9C%A8Windows%E4%B8%8B%E7%9B%B4%E6%8E%A5%E8%BF%90%E8%A1%8CLinux" style="box-sizing: border-box; background-color: transparent; color: #0366d6; text-decoration-line: none;">https://git-scm.com/），</a></span><strong><span style="font-family: verdana, &quot;courier new&quot;;">那么就可以在Windows下直接运行Linux Shell</span><span style="box-sizing: border-box;">，只需要将文件后缀命名为sh即可</span></strong>。</p><p data-source-line="29" style="box-sizing: border-box; margin-top: 0px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff; margin-bottom: 0px !important;">问过周边友人是否知道这一功能，都说知道，只是没有广播而已，害我研究很久&#8230;&#8230;</p><p data-source-line="29" style="box-sizing: border-box; margin-top: 0px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff; margin-bottom: 0px !important;"></p><p data-source-line="29" style="box-sizing: border-box; margin-top: 0px; color: #24292e; font-family: -apple-system, BlinkMacSystemFont, 微软雅黑, &quot;PingFang SC&quot;, Helvetica, Tahoma, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, SimSun, 宋体, Heiti, 黑体, sans-serif; background-color: #ffffff; margin-bottom: 0px !important;"></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/215373.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-11-27 15:15 <a href="http://www.cppblog.com/sunicdavy/archive/2017/11/27/215373.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Golang 热更新研究笔记</title><link>http://www.cppblog.com/sunicdavy/archive/2017/07/06/215057.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 06 Jul 2017 04:47:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/07/06/215057.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/215057.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/07/06/215057.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/215057.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/215057.html</trackback:ping><description><![CDATA[<p data-source-line="1" style="box-sizing: border-box; margin-bottom: 16px; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 21px; widows: 1; background-color: #ffffff; margin-top: 0px !important;">本文主要研究游戏服务器带状态的热更新需求 http的无状态热更新需求已经有成熟方案, 故不在本文描述范围</p><h1><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#基本概念" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>基本概念</h1><ul data-source-line="6" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 21px; widows: 1; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Golang的热更新采用什么机制?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">使用go1.8提供的plugin包机制实现</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">plugin包本身设计的目的是热更新么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">plugin包其实只是支持将代码分别编译为多个动态库,动态加载后运行 并不能完全支持类似C/C++的动态库方式处理代码</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">带状态的进程热更新的基本概念及范围是什么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">数据部分(model)不更新, 只更新逻辑部分(函数)</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">表格和配置更新算热更新么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">算, 但不是本文描述范围</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">热更新能在windows上使用么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">不支持</p></li></ul><h1><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#代码及结构" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>代码及结构</h1><ul data-source-line="29" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 21px; widows: 1; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">我能将原来一个exe的代码编译为so提供给plugin使用么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">可以, 但是必须仍然保留main包作为插件入口, 并在main包内添加提供给plugin调用入口.</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">如果动态库中没有main包, 编译出的so能用么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">不能, 包必须包含main, 否则输出的是.a的文件, plugin包加载会报错</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">动态库中, 非main包的的代码修改能做热更新么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><span style="box-sizing: border-box; font-weight: 600;">不能</span>!(崩溃了吧, 我提了一个issue:&nbsp;<a href="https://github.com/golang/go/issues/20554" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none; background-color: transparent;">https://github.com/golang/go/issues/20554</a>)</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">如果确实做了修改, 底层会报错: plugin was built with a different version of package</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">解决方法: 修改plugin包底层实现并重新编译 打开runtime/plugin.go, 注释以下代码 for _, pkghash := range md.pkghashes { if pkghash.linktimehash != *pkghash.runtimehash { return "", nil, pkghash.modulename } } 执行/usr/local/go/run.bash 重编译+测试</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">代码中哪些可以被更新? 方法可以被更新么? 闭包呢?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">只能更新拥有静态地址的结构.例如: 包级别函数(类似于静态函数)</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">例如: svc_0.so里有一个Foo函数, svc_1.so修改了Foo函数实现, 热更新可实现</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><span style="box-sizing: border-box; font-weight: 600;">闭包=函数+捕获变量</span>, 实际上是一个动态结构, 没有静态地址, 无法被更新</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">各种包级别变量, 结构体变量, 结构体方法, 局部变量均不能被热更新, 但是变量值不会被影响</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">新增结构可以被运行</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">使用结构体方法调用了包级别函数, 包级别函数能被更新么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">可以, 虽然方法不能被更新, 但方法被调用的包级别函数的地址是固定的, 所以可以被热更新</p></li><li style="box-sizing: border-box; margin-top: 0.25em;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">init包初始化函数能用么? 能被热更新么?</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">官方这样描述:</p><pre data-source-line="71" style="box-sizing: border-box; font-stretch: normal; font-size: 11.9px; line-height: 1.45; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; word-wrap: normal; margin-top: 0px; margin-bottom: 16px; padding: 16px; overflow: auto; border-radius: 3px; background-color: #f7f7f7;"><code style="box-sizing: border-box; display: inline; overflow: visible; padding: 0px; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 11.9px; border-radius: 3px; word-break: normal; border: 0px; line-height: inherit; word-wrap: normal; background: 0px 0px transparent;"><span style="box-sizing: border-box; font-weight: 700;">When</span> a plugin <span style="box-sizing: border-box; font-weight: 700;">is</span> first opened, the init functions <span style="box-sizing: border-box; font-weight: 700;">of</span> <span style="box-sizing: border-box; font-weight: 700;">all</span> packages <span style="box-sizing: border-box; font-weight: 700;">not</span> already part <span style="box-sizing: border-box; font-weight: 700;">of</span> the program are called. The main <span style="box-sizing: border-box; font-weight: 700;">function</span> <span style="box-sizing: border-box; font-weight: 700;">is</span> <span style="box-sizing: border-box; font-weight: 700;">not</span> run. A plugin <span style="box-sizing: border-box; font-weight: 700;">is</span> only initialized once, <span style="box-sizing: border-box; font-weight: 700;">and</span> cannot be closed</code></pre><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">插件第一次被打开时, 其关联的, 没有成为程序的一部分的所有的包的init函数将被调用. 插件的main函数不会被调用. 插件只会被初始化一次, 不能被关闭</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">因此, 需要手动将init函数改成自己的函数, 统一在so的main包里调用</p></li></ul><h1><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#编译" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>编译</h1><ul data-source-line="80" style="box-sizing: border-box; margin-top: 0px; padding-left: 2em; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 21px; widows: 1; background-color: #ffffff; margin-bottom: 0px !important;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">如何编译获得plugin包支持的动态库</p><pre data-source-line="82" style="box-sizing: border-box; font-stretch: normal; font-size: 11.9px; line-height: 1.45; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; word-wrap: normal; margin-top: 0px; margin-bottom: 16px; padding: 16px; overflow: auto; border-radius: 3px; background-color: #f7f7f7;"><code style="box-sizing: border-box; display: inline; overflow: visible; padding: 0px; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 11.9px; border-radius: 3px; word-break: normal; border: 0px; line-height: inherit; word-wrap: normal; background: 0px 0px transparent;">SVCNAME=<span style="box-sizing: border-box; color: teal;">$1</span> SVCVER=<span style="box-sizing: border-box; color: teal;">$2</span> TIMESTAMP=`date <span style="box-sizing: border-box; color: #dd1144;">'+%Y%m%d_%H%M%S'</span>` go build -v -buildmode=plugin --ldflags=<span style="box-sizing: border-box; color: #dd1144;">"-pluginpath=${SVCNAME}_${TIMESTAMP}"</span> -o ${SVCNAME}<span style="box-sizing: border-box; color: teal;">_</span>${SVCVER}.so ${SVCNAME}</code></pre><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">-buildmode=plugin是重要参数</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">--ldflags里的-pluginpath的作用是: 每次编译的内部识别路径都是不同的, 避免重复加载的警告</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">参考:&nbsp;<a href="https://github.com/golang/go/issues/19004" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none; background-color: transparent;">https://github.com/golang/go/issues/19004</a></p></li></ul><img src ="http://www.cppblog.com/sunicdavy/aggbug/215057.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-07-06 12:47 <a href="http://www.cppblog.com/sunicdavy/archive/2017/07/06/215057.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Visual Studio Code调试Golang工程</title><link>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 20 Apr 2017 04:52:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214874.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214874.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214874.html</trackback:ping><description><![CDATA[<h1 id="使用visual-studio-code调试golang工程" style="box-sizing: border-box; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 2.25em/1.2 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; margin: 0px 0px 16px; widows: 1; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="1">使用Visual Studio Code调试Golang工程</h1>
<h2 id="关键字" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="3"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#关键字"></a>关键字</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="4"><li style="box-sizing: border-box">最简单的调试攻略</li><li style="box-sizing: border-box">多项目调试, 适用个人开发和项目开发</li><li style="box-sizing: border-box">无需修改系统环境变量</li></ul>
<h2 id="准备vscode" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="8"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#准备vscode"></a>准备VSCode</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="10">在官网下载最新版的VSCode:</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="12"><a style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="https://code.visualstudio.com/">https://code.visualstudio.com/</a></p>
<h2 id="安装golang插件" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="14"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#安装golang插件"></a>安装Golang插件</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="15"><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">打开扩展面板</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">VSCode-&gt;查看-&gt;扩展</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">找到Go插件 在搜索框里输入Go, 找到第二行写有 Rich Go language support for Visual Studio Code的插件, 点击安装</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">注意不是排名最高的</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">重启编辑器</p></li></ul>
<h2 id="配置启动项" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="26"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#配置启动项"></a>配置启动项</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="27"><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">打开调试面板</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">VSCode-&gt;查看-&gt;调试</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">添加调试目标</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">在"没有调试"的下拉框中点击"添加配置.."</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">添加目标调试配置</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">例子:</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; margin-top: 0px; padding-right: 16px; background-color: rgb(247,247,247); font-stretch: normal; border-radius: 3px" data-source-line="38"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">{
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">version</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"0.2.0"</span></span>,
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">configurations</span>": <span class="hljs-value" style="box-sizing: border-box">[
        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"Launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${fileDirname}"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>}
    ]
</span>}</code></pre></li></ul>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="61">其中: "port", "host"都是go插件自动生成的</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="63">"env"为设置环境变量, 设置为你的工程目录就可以(包含bin, src的文件夹)</p>
<h2 id="准备调试插件" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="66"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#准备调试插件"></a>准备调试插件</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="70">此时找到main.go按F5, 会报错提示:</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="71"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">Failded to continue:"Cannot find Delve debugger. <span class="hljs-operator" style="box-sizing: border-box"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">Install</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">from</span> https://github.com/derekparker/delve &amp; ensure it <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">is</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">in</span> your <span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"GOPATH/bin"</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">or</span> <span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"PATH"</span></span></code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="75">我们使用go命令行编译调试器</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="76"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">go</span> <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(0,134,179)">get</span> github.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">com</span>/derekparker/delve/cmd/dlv</code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="80">将dlv调试器放在GOPATH(工程目录)的bin目录下</p>
<h2 id="开始调试" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="82"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#开始调试"></a>开始调试</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="84">选中要调试的main.go, 点击F5, 既可以开始调试</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="86">调试快捷键和Visual Studio系一致</p>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="87"><li style="box-sizing: border-box">F9 切换断点</li><li style="box-sizing: border-box">F10 Step over</li><li style="box-sizing: border-box">F11 Step in</li><li style="box-sizing: border-box">Shift+F11 Step out</li></ul>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="92">注意点</p>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="93"><li style="box-sizing: border-box">某些结构体成员无法直接显示时, 可以直接选中变量名, 添加到监视, 或者右键点击: "调试:求值"</li></ul>
<h2 id="多项目调试" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="96"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#多项目调试"></a>多项目调试</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="98">在launch.json中可以添加多组调试入口, 通过调试面板中选中对应的配置开启不同目标的调试</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="100"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">{
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">version</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"0.2.0"</span></span>,
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">configurations</span>": <span class="hljs-value" style="box-sizing: border-box">[
        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"client"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${fileDirname}"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>},

        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"server"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${workspaceRoot}/src/server"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>}
    ]
</span>}</code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="138">"program"中的"${fileDirname}"是以当前选中文件作为启动点</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="140"><strong style="box-sizing: border-box; font-weight: bolder">更建议使用"program"的"${workspaceRoot}", 以包名作为启动点的方式进行配置</strong></p>
<h2 id="参考链接" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="144"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#参考链接"></a>参考链接</h2>
<p style="box-sizing: border-box; margin-bottom: 0px !important; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="146"><a style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="https://code.visualstudio.com/Docs/editor/debugging">https://code.visualstudio.com/Docs/editor/debugging</a></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-04-20 12:52 <a href="http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MMO相位技术研究</title><link>http://www.cppblog.com/sunicdavy/archive/2017/04/08/214817.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sat, 08 Apr 2017 06:41:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/04/08/214817.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214817.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/04/08/214817.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214817.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214817.html</trackback:ping><description><![CDATA[<h1>相位技术</h1><p data-source-line="3" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">相位技术大规模出现在魔兽世界WLK版本, 现在应用已经广泛应用在各种MMORPG游戏中. 下面对相位技术的做法进行简单归纳汇总</p><h2><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#表现分类" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>表现分类</h2><h3><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#副本相位" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>副本相位</h3><p data-source-line="10" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">早期副本的出现, 避免抢怪问题. 所以, 副本其实本身就是一种相位技术. 只不过实现时, 我们一般会将小队和怪物直接预分配在独立的一个副本实例中(所以副本原文也是实例的意思)</p><h3><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#分线相位" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>分线相位</h3><p data-source-line="15" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">相位技术还没有正式命名时, 同一个场景, 玩家进到不同的分线看到的玩家不一样, 也是属于相位的一种. 当然, 如果是组队玩家, 服务器默认会分配所有队伍玩家在同一个线(位面)</p><h3><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#真相位" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>真相位</h3><p data-source-line="20" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">副本相位和分线相位其实都是静态相位, 一旦进入, 中途不会有切换或者混合查看的过程. 真相位可以在一个场景中,动态切换相位, 相位内和相位外所以不同</p><p data-source-line="22" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">我们常见的真相位表现为:&nbsp;<span style="box-sizing: border-box; font-weight: bolder;">相位中的角色+玩家+队伍成员</span></p><p data-source-line="24" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">在护送任务时, 还会在上面所见角色中叠加世界中的所有角色, 也就是说, 你和队伍成员可以看到的角色, 其他人看不到, 其他人也看不到你和你的队伍成员</p><hr style="box-sizing: content-box; overflow: hidden; height: 4px; padding: 0px; margin: 16px 0px; border-width: 0px; border-style: initial; border-bottom-color: #eeeeee; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background: #e7e7e7;" /><p data-source-line="28" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">为了清晰的简单的描述, 我为相位创建如下概念与名词</p><h2><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#相位客体" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>相位客体</h2><p data-source-line="33" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">表现为除玩家外的角色(怪物,交互物体与相位可见场景)</p><h3><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#私有客体" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>私有客体</h3><p data-source-line="37" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">这是最常见的一种相位内角色</p><ul data-source-line="39" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">持有变量</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">取PhasingID时,为PhasingTargetID</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">生成规则</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">当玩家开启相位后, 在玩家相位内<span style="box-sizing: border-box; font-weight: bolder;">生成的角色</span>为私有客体.</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">此时, 将 PhasingTargetID设置为相位生成者的实例ID</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">删除规则</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">如果玩家退出相位, 私有客体会存在一段时间或按照需求删除</p></li></ul><h3><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#公共客体" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>公共客体</h3><p data-source-line="55" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">一般指提前放置在场景中, 世界内不可见, 但是能被同相位玩家可见,且同相位玩家都可以互相可见 比如: 只要接了同一个任务的玩家, 都可以看到的NPC</p><ul data-source-line="58" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">持有变量</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">取PhasingID时,为PublicPhasingID</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">生成规则</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">通过场景编辑器, 放置角色时, 设置其可被观察到的任务ID</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">角色被加载后, 将任务ID设置到StaticPhasingID</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">删除规则</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">场景删除, 角色删除</p></li></ul><h2><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#相位主体" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>相位主体</h2><p data-source-line="75" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">包含玩家与同队伍玩家</p><ul data-source-line="78" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;"><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">开启相位后, 可见私有客体+公有客体</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">队长视为相位主体, 单人时, 自己为队长</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">队伍其他成员共享队长的私有相位客体</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">队伍其他成员根据自己的PublicPhasingID匹配目标对象的PublicPhasingID时可互相可见</p></li><li style="box-sizing: border-box;"><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">持有变量</p></li></ul><p data-source-line="88" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">相位开启时, 取PhasingID时, 为角色实例ID</p><p data-source-line="90" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">相位关闭时, 取PhasingID时, 为0</p><p data-source-line="92" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">PublicPhasingID</p><h2><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#可见规则" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>可见规则</h2><p data-source-line="98" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">当两个角色的PhasingID相等时, 主体与私有客体互相可见</p><p data-source-line="100" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">当两个角色的PublicPhasingID相等时, 主体与公有客体互相可见</p><p data-source-line="102" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff;">可以通过开关设置, 是否在可见的相位客体基础上, 叠加世界角色(护送任务)</p><h2><a href="file:///C:/Users/Davy/AppData/Local/Youdao/YNote/markdown/index.html#约束" style="box-sizing: border-box; color: #4078c0; text-decoration-line: none;"></a>约束</h2><ul data-source-line="106" style="box-sizing: border-box; margin-top: 0px; padding-left: 2em; color: #333333; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; line-height: 22.4px; widows: 1; background-color: #ffffff; margin-bottom: 0px !important;"><li style="box-sizing: border-box;">玩家同时只能看见1个相位</li></ul><img src ="http://www.cppblog.com/sunicdavy/aggbug/214817.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-04-08 14:41 <a href="http://www.cppblog.com/sunicdavy/archive/2017/04/08/214817.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go语言与构造函数</title><link>http://www.cppblog.com/sunicdavy/archive/2016/12/01/214457.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 01 Dec 2016 02:45:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2016/12/01/214457.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214457.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2016/12/01/214457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214457.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214457.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-top: 0px !important;">Golang中没有设计构造函数. 取而代之的, 设计Golang的大师希望你用普通函数去实现构造的任务.&nbsp;<br />一直只是觉得这只是体现Golang这门新语言的精简设计之道, 直到自己实现编译器后才发现构造函数的设计本身是值得商榷的</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">我们先看下构造函数的规则</p><h1>构造函数调用规则</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">构造参数量: 0表示没有构造函数, 1表示有构造函数0个参数</p><table style="margin: 10px 0px; border-collapse: collapse; border-style: solid; border-color: #bbbbbb; padding: 0px; color: #000000; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><thead><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: white;"><th style="padding: 6px 13px; border-collapse: collapse; border: 1px solid #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">本类构造</th><th style="padding: 6px 13px; border-collapse: collapse; border: 1px solid #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">父类构造</th><th style="padding: 6px 13px; border-collapse: collapse; border: 1px solid #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">处理方法</th></tr></thead><tbody><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: white;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">0</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">0</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">不处理</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: #f8f8f8;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">0</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">调本类ctor</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: white;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">0</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">调父类ctor</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: #f8f8f8;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">调本类ctor, 本类ctor调父类ctor</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: white;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">2</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">调本类ctor, 本类ctor调父类ctor</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: #f8f8f8;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">1</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">2</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">报错, 手动调父类ctor</td></tr><tr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc; margin: 0px; padding: 0px; background-color: white;"><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">2</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">2</td><td style="padding: 6px 13px; border-collapse: collapse; border-style: solid; border-color: #cccccc; height: 28px; word-break: break-all; box-sizing: border-box; outline: none; margin: 0px; font-size: 14px;">报错, 手动调父类ctor</td></tr></tbody></table><h1>普通函数重载规则</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">实际只用考虑最典型的一种行为: 实例化子类, 转为父类调用方法, 这个时候</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">如果方法是override, 调用的是子类</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">如果方法是virutal或者不指明, 调用的是父类</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">整个重载过程, 子类绝对不会隐式调用父类的行为</p><h1>需要构造函数么?</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">构造函数的优点</p><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;">本身属于一种特殊的成员函数</li><li style="margin: 5px 0px;">编译器帮你自动传导调用父级</li></ul><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">构造函数的缺点</p><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;">隐式的调用规则</li><li style="margin: 5px 0px;">虽然属于成员函数, 但是与其他成员函数调用规则完全不同, 需要特殊记忆</li><li style="margin: 5px 0px;">带参数的构造函数, 在父类参数多于子类时, 需要引用复杂语法来实现父级构造调用</li></ul><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">其实我们对初始化函数的需求只有1条: 自定义</p><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;">所以, 可以理解Golang不加入构造函数的设计是正确的&nbsp;<br />即: 简单, 清晰, 有规律</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2016-12-01 10:45 <a href="http://www.cppblog.com/sunicdavy/archive/2016/12/01/214457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go语言的冷知识</title><link>http://www.cppblog.com/sunicdavy/archive/2016/11/02/214375.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 02 Nov 2016 03:09:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2016/11/02/214375.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214375.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2016/11/02/214375.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214375.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214375.html</trackback:ping><description><![CDATA[<h1>append, map, len不是关键字</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">他们其实还是类库功能, 都在buildin包里的, 系统默认给你做了个</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #1e347b; margin-top: 0px;">import</span><span style="color: #93a1a1;">(</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: #93a1a1;">.</span> <span style="color: #dd1144;">"buildin"</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">)</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">将buildin的包内容都映射到全局而已, 其实你也可以用自己的包这么做</p><h1>打印的另一种写法</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">想跟脚本一样调试打印数据么?</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">println</span><span style="color: #93a1a1;">(</span><span style="color: #dd1144;">"hello world"</span><span style="color: #93a1a1;">)</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">无需包含任何包, 因为它在buildin包里</p><h1>iota不是黑科技</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">这是在buildin包里的定义</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">// iota is a predeclared identifier representing the untyped integer ordinal</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">// number of the current const specification in a (usually parenthesized)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">// const declaration. It is zero-indexed.</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #1e347b; margin-top: 0px;">const</span><span style="color: #48484c;"> iota </span><span style="color: #93a1a1;">=</span> <span style="color: #195f91;">0</span> <span style="color: #93a1a1;">// Untyped int.</span></code></li></ol></pre><h1>其实go是有泛型概念的</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">想想map和数组的定义&nbsp;<br />只是泛型没有开放给用户用而已(只许XX放火,不许XX点灯)</p><h1>map是支持多个key的, 而且很方便</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">还在为多个key转id的复杂算法而头疼么?</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">type myKey </span><span style="color: #1e347b;">struct</span><span style="color: #93a1a1;">{</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    number </span><span style="color: #1e347b;">int</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    str </span><span style="color: #1e347b;">string</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func main</span><span style="color: #93a1a1;">(){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    t </span><span style="color: #93a1a1;">:=</span><span style="color: #48484c;"> map</span><span style="color: #93a1a1;">[</span><span style="color: #48484c;"> myKey</span><span style="color: #93a1a1;">]</span> <span style="color: #1e347b;">int</span> <span style="color: #93a1a1;">{</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">        myKey</span><span style="color: #93a1a1;">{</span> <span style="color: #195f91;">2</span><span style="color: #93a1a1;">,</span> <span style="color: #dd1144;">"world"</span><span style="color: #93a1a1;">}:</span> <span style="color: #195f91;">1</span><span style="color: #93a1a1;">,</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: #93a1a1;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    fmt</span><span style="color: #93a1a1;">.</span><span style="color: teal;">Println</span><span style="color: #93a1a1;">(</span><span style="color: #48484c;">t</span><span style="color: #93a1a1;">[</span><span style="color: #48484c;">myKey</span><span style="color: #93a1a1;">{</span><span style="color: #195f91;">2</span><span style="color: #93a1a1;">,</span> <span style="color: #dd1144;">"world"</span><span style="color: #93a1a1;">}])</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">输出:</span> <span style="color: #195f91;">1</span></code></li></ol></pre><h1>枚举是可以转成string的</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">默认定义一个枚举</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">type </span><span style="color: teal;">MyConst</span> <span style="color: #1e347b;">int</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #1e347b; margin-top: 0px;">const</span> <span style="color: #93a1a1;">(</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: teal;">MyConst_A</span> <span style="color: teal;">MyConst</span> <span style="color: #93a1a1;">=</span><span style="color: #48484c;"> iota</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: teal;">MyConst_B</span> <span style="color: teal;">MyConst</span> <span style="color: #93a1a1;">=</span><span style="color: #48484c;"> iota</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func main</span><span style="color: #93a1a1;">(){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    fmt</span><span style="color: #93a1a1;">.</span><span style="color: teal;">Println</span><span style="color: #93a1a1;">(</span><span style="color: teal;">MyConst_A</span><span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">输出: 0&nbsp;<br />如果我们想自动化输出MyConst_A字符串时&nbsp;<br />就需要使用golang的一个工具链:golang.org/x/tools/cmd/stringer&nbsp;<br />将其下载, 编译成可执行工具后, 对代码进行生成&nbsp;<br />生成的代码会多出一个xx_string.go&nbsp;<br />里面就是枚举的String()string 函数</p><h1>临时转换一个接口并调用的方法</h1><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">type love </span><span style="color: #1e347b;">struct</span><span style="color: #93a1a1;">{</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func </span><span style="color: #93a1a1;">(</span><span style="color: #1e347b;">self</span><span style="color: #93a1a1;">*</span><span style="color: #48484c;">love</span><span style="color: #93a1a1;">)</span><span style="color: #48484c;">foo</span><span style="color: #93a1a1;">(){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    fmt</span><span style="color: #93a1a1;">.</span><span style="color: teal;">Println</span><span style="color: #93a1a1;">(</span><span style="color: #dd1144;">"love"</span><span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func main</span><span style="color: #93a1a1;">(){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: #1e347b;">var</span><span style="color: #48484c;"> chaos </span><span style="color: #1e347b;">interface</span><span style="color: #93a1a1;">{}</span> <span style="color: #93a1a1;">=</span> <span style="color: #1e347b;">new</span><span style="color: #93a1a1;">(</span><span style="color: #48484c;">love</span><span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    chaos</span><span style="color: #93a1a1;">.(</span><span style="color: #1e347b;">interface</span><span style="color: #93a1a1;">{</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">        foo</span><span style="color: #93a1a1;">()</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: #93a1a1;">}).</span><span style="color: #48484c;">foo</span><span style="color: #93a1a1;">()</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li></ol></pre><h1>Golang的receiver实际上就是this的变种实现</h1><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func</span><span style="color: #93a1a1;">(</span> <span style="color: #1e347b;">self</span><span style="color: #93a1a1;">*</span><span style="color: teal;">MyStruct</span><span style="color: #93a1a1;">)</span><span style="color: #48484c;"> foo</span><span style="color: #93a1a1;">(</span><span style="color: #48484c;"> p </span><span style="color: #1e347b;">int</span> <span style="color: #93a1a1;">){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">写不惯receiver的写法? 如果这样改下呢?</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func foo</span><span style="color: #93a1a1;">(</span> <span style="color: #1e347b;">self</span> <span style="color: #93a1a1;">*</span><span style="color: teal;">MyStruct</span><span style="color: #93a1a1;">,</span><span style="color: #48484c;"> p </span><span style="color: #1e347b;">int</span> <span style="color: #93a1a1;">){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">所以为什么说Golang还是一个C语言嘛</p><h1>关于内存分配&#8230;</h1><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;">new 传入Type类型, 返回*Type类型</li><li style="margin: 5px 0px;">make 可以在分配数组时设置预分配大小, new不可以</li><li style="margin: 5px 0px;">make 能分配数组,map, 但不能分配结构体和原始类型</li><li style="margin: 5px 0px;">new 能分配数组, map, 结构体和原始类型等的所有类型</li><li style="margin: 5px 0px;">&amp;Type等效于new</li><li style="margin: 5px 0px;">切片不需要分配内存(make,new), 直接声明就可以了&#8230;</li></ul><h1>Golang的反射无法通过一个类型名, 创建其实例</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">C#有Assembly概念, 可以在一个Assembly里搜索, 创建实例</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">Golang是静态类型语言, 如果需要, 只能注册你需要创建的结构体, 然后将注册好的map用于创建</p><h1>Golang可以替换Python来进行复杂的工具流程处理</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">如果你需要跨平台的工具流程处理, 对Python不熟悉, 可以使用</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">go run yourcode</span><span style="color: #93a1a1;">.</span><span style="color: #48484c;">go </span><span style="color: #93a1a1;">参数</span><span style="color: #195f91;">1</span> <span style="color: #93a1a1;">参数</span><span style="color: #195f91;">2</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">方式来进行工具处理&nbsp;<br />觉得慢, 可以编译成可执行文件</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">这样做的好处: 如果有些类库本身就是go写的, Python想使用是很麻烦的, 而Golang来写则轻而易举</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">例子: 通过go的protobuf库, 对一些文件进行处理</p><h1>Golang可以自动持有方法的接收者实例</h1><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">type myType </span><span style="color: #1e347b;">struct</span><span style="color: #93a1a1;">{</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func </span><span style="color: #93a1a1;">(</span><span style="color: #1e347b;">self</span><span style="color: #93a1a1;">*</span><span style="color: #48484c;">myType</span><span style="color: #93a1a1;">)</span><span style="color: #48484c;"> foo</span><span style="color: #93a1a1;">(</span><span style="color: #48484c;"> p </span><span style="color: #1e347b;">int</span><span style="color: #93a1a1;">){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    fmt</span><span style="color: #93a1a1;">.</span><span style="color: teal;">Println</span><span style="color: #93a1a1;">(</span><span style="color: #dd1144;">"hello"</span><span style="color: #93a1a1;">,</span><span style="color: #48484c;"> p </span><span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">func main</span><span style="color: #93a1a1;">(){</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"> <span style="color: #1e347b;">var</span><span style="color: #48484c;"> callback func</span><span style="color: #93a1a1;">(</span> <span style="color: #1e347b;">int</span> <span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    ins </span><span style="color: #93a1a1;">:=</span> <span style="color: #1e347b;">new</span><span style="color: #93a1a1;">(</span><span style="color: #48484c;">myType</span><span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    callback </span><span style="color: #93a1a1;">=</span><span style="color: #48484c;"> ins</span><span style="color: #93a1a1;">.</span><span style="color: #48484c;">foo</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">    callback</span><span style="color: #93a1a1;">(</span> <span style="color: #195f91;">100</span> <span style="color: #93a1a1;">)</span></code></li><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #93a1a1; margin-top: 0px;">}</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">做过lua的C++代码绑定的童鞋都清楚: lua只支持外部静态或者全局函数调用&nbsp;<br />如果要进行C++类成员函数调用时, 要自己处理this和成员函数&nbsp;<br />这种技巧因为早起编译器的虚表不同平台实现细节不统一需要专门处理&nbsp;<br />后面跨平台虚表统一后, 类成员函数的调用写法也是很恶心复杂的&nbsp;<br />但是Golang的小白式用法, 直接吊打C++, 甚至C#复杂的delegate</p><h1>LiteIDE篇: 多开秘籍</h1><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;"><p style="margin: 0px;">找到 菜单-&gt;查看-&gt;选项-&gt;通用-&gt;存储-&gt;存储设置到本地ini文件</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">关闭LiteIDE</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">复制LiteIDE整个目录, 命名文件夹为你的工程名</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">每个工程所在的LiteIDE的配置将是独立的, 不会互相干扰</p></li></ul><h1>LiteIDE篇: 测试程序也是可以调试的</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">别以为程序一定要是main开始的才可以调试</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">Golang的测试程序虽然都是一个个Test开头的函数,但执行go test时, 还是有main入口</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">在LiteIDE中, 可以在 调试-&gt;开始调试测试程序里进行测试程序调试</p><h1>LiteIDE篇: 在Windows上可以输出linux可执行文件</h1><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;">go的工具链默认支持交叉编译&nbsp;<br />在LiteIDE中, 可以通过切换输出平台, 输出不同平台的可执行文件</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2016-11-02 11:09 <a href="http://www.cppblog.com/sunicdavy/archive/2016/11/02/214375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用delve调试Golang程序技巧</title><link>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sat, 03 Sep 2016 10:12:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214254.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214254.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214254.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-top: 0px !important;"></p><p style="margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-top: 0px !important;">Google官方为Golang的调试例子默认使用了gdb</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">然而, 使用gdb调试go程序会遇到goroutine的各类问题, 因为gdb不懂go</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">因此, 这里使用delve黑科技来进行Golang的程序调试</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">纯命令行调试方法在网上很容易搜索到, 本文主要以LiteIDE来进行程序调试</p><h1>关闭编译器优化</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">正常go build/install出的go程序是完全优化过的, 强行使用调试器挂接调试时, 某些local变量/lamda表达式捕获的变量会直接进入寄存器, 无法使用调试器查看</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">删掉所有的pkg, 为build或install参数加入关闭编译器优化的参数 -gcflags "-N -l"</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">例如:</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">   go install </span><span style="color: #93a1a1;">-</span><span style="color: #48484c;">gcflags </span><span style="color: #dd1144;">"-N -l"</span><span style="color: #48484c;"> svc\gamesvc</span></code></li></ol></pre><h2>delve调试器安装方法</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE自带了gdb, 但是没有delve调试器, 需要自行安装, 命令如下</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">go </span><span style="color: #1e347b;">get</span><span style="color: #48484c;"> github</span><span style="color: #93a1a1;">.</span><span style="color: #48484c;">com</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">derekparker</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">delve</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">cmd</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">dlv</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">delve调试器会被放到你的GOPATH/bin下</p><h1>LiteIDE中的delve调试器配置</h1><h2>选择调试器</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">在LiteIDE菜单中选择 调试-&gt;debugger/delve</p><h2>delve环境变量设置</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">这个时候, LiteIDE依然找不到delve, 因为它不在环境变量PATH中, 这里无需修改环境变量, 只需要LiteIDE的环境配置</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">在LiteIDE菜单中选择 查看-&gt;编辑当前环境, 在弹出的文档中修改</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">PATH</span><span style="color: #93a1a1;">=</span><span style="color: #48484c;">c</span><span style="color: #93a1a1;">:</span><span style="color: #48484c;">\mingw32\b</span><span style="color: #1e347b;">in</span><span style="color: #93a1a1;">;%</span><span style="color: #48484c;">GOROOT</span><span style="color: #93a1a1;">%</span><span style="color: #48484c;">\b</span><span style="color: #1e347b;">in</span><span style="color: #93a1a1;">;%</span><span style="color: #48484c;">PATH</span><span style="color: #93a1a1;">%;</span><span style="color: #48484c;">c</span><span style="color: #93a1a1;">:</span><span style="color: #48484c;">\y</span><span style="color: #1e347b;">our</span><span style="color: #48484c;">\path\to\delve</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">去掉PATH前的注释#, 在%PATH%添加分号, 然后和你到delve调试器的路径</p><h2>开始调试</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">选择你的工程, 点击F5, 进入调试模式</p><h2>调试器显示变量值</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;"><img src="file:///D:/Documents/My%20Knowledge/temp/6f9eec62-fe6c-4f28-89ad-acbade688a96/4/index_files/2be001ea-878e-4e38-9209-76ef93d427f0.png" style="border: 0px; max-width: 100%; margin: 2px 0px; height: auto !important;"  alt="" /></p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE使用delve调试时, 无法在 变量 监视等窗口中正确捕捉delve调试返回数据(因为格式太复杂了&#8230;)</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">没关系, 我们使用命令行配合显示即可</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE控制台或调试输出窗口在delve调试时, 实际上是一个标准命令行&nbsp;<br />命令如下</p><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;"><p style="margin: 0px;">p 变量名可以查看变量值</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">locals查看局部变量</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">ls可查看当前文件</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">stack查看栈</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">help可以查看各种帮助</p></li></ul><h2>调试外部程序</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">如果你的程序是外部程序, 或者使用go install安装到GOPATH/bin目录的程序, 那么使用delve调试器启动程序时, 可能会碰到启动路径错误的问题</p><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;">使用LiteIDE菜单 调试-&gt;调试其他应用程序&#8230; 填入你要调试程序的路径以及工作目录, 可以解决这个问题</p><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;"></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214254.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2016-09-03 18:12 <a href="http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>