锘??xml version="1.0" encoding="utf-8" standalone="yes"?>BlogJava-灏戝勾闃垮http://www.5432034.com/stevenjohn/閭簺闈掓槬鐨勫瞾鏈?/description>zh-cnSat, 23 Mar 2019 12:24:33 GMTSat, 23 Mar 2019 12:24:33 GMT60濡備綍鐢秷鎭郴缁熼?#22446;?#23941;垎甯冨紡浜嬪姟锛?/title><link>http://www.5432034.com/stevenjohn/archive/2018/01/04/433004.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Wed, 03 Jan 2018 16:01:00 GMT</pubDate><guid>http://www.5432034.com/stevenjohn/archive/2018/01/04/433004.html</guid><wfw:comment>http://www.5432034.com/stevenjohn/comments/433004.html</wfw:comment><comments>http://www.5432034.com/stevenjohn/archive/2018/01/04/433004.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.5432034.com/stevenjohn/comments/commentRss/433004.html</wfw:commentRss><trackback:ping>http://www.5432034.com/stevenjohn/services/trackbacks/433004.html</trackback:ping><description><![CDATA[     鎽樿: 鍓?#38339;?#28699;愪粠鏀粯瀹濊浆璐?涓囧潡閽卞埌浣欓瀹?#38171;?#26473;欐槸鏃?#29999;?#37922;熸椿鐨?#28051;浠舵櫘閫?#28751;?#27996;?#38171;?#28003;?#28003;?#28051;?#27996;?#38001;旂綉鐮斿彂浜哄憳鐨?#38001;?#28051;氱梾锛屾垜灏辨濊?#37824;粯瀹?#37813;櫎1涓囦?#23338;悗锛?#28641;傛灉?#37108;缁熸?#20635;帀鎬庝箞鍔?#38171;?#26473;欐椂浣欓瀹濊处鎴峰?#33333;病鏈?#28583;?#37716;?涓?#38171;屾?#29256;嵁灏变?#27692;?#34425;幇涓?#28051;鑷寸姸鍐?#27996;?#37510;備笂杩板満鏅湪鍚?#28051;?#32491;诲瀷鐨勭郴缁?#28051;?#38318;借兘鎵惧埌鐩镐技褰?#28699;?#38171;屾瘮濡傚湪鐢靛晢?#37108;缁?#28051;?#38171;?#35120;?#37832;?#37922;?#37812;?#28051;嬪崟鍚?#38171;岄櫎浜嗗湪璁崟琛?#37819;?#37711;?#28051;鏉?#29825;板綍澶?#38171;屽搴斿晢鍝佽鐨?#26473;?#28051;晢鍝?#37825;?#38322;忓繀椤?#37713;?鍚?#38171;屾庝箞淇濊瘉锛?#38171;佸湪?#24755;绱?#39582;垮?#23108;郴缁?#28051;?#38171;?#35120;?#37922;?#37812;?#37904;?#37713;?#37836;愬?#22446;鍚?#38171;岄櫎浜嗗湪鐐?#37713;?..  <a href='http://www.5432034.com/stevenjohn/archive/2018/01/04/433004.html'>闃呰鍏枃</a><img src ="http://www.5432034.com/stevenjohn/aggbug/433004.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.5432034.com/stevenjohn/" target="_blank">abin</a> 2018-01-04 00:01 <a href="http://www.5432034.com/stevenjohn/archive/2018/01/04/433004.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>寰?#37832;嶅姟?#32045;鐐圭?#34425;偣http://www.5432034.com/stevenjohn/archive/2017/12/31/432996.htmlabinabinSun, 31 Dec 2017 08:41:00 GMThttp://www.5432034.com/stevenjohn/archive/2017/12/31/432996.htmlhttp://www.5432034.com/stevenjohn/comments/432996.htmlhttp://www.5432034.com/stevenjohn/archive/2017/12/31/432996.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/432996.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/432996.html寰?#37832;嶅姟鏋舵瀯閲?#37922;Scale Cube鏂规?#26337;璁?#25652;?#37922;?#37835;舵瀯锛?#28751;?#25652;?#37922;?#37832;嶅姟鎸?#37716;熻兘鎷嗗垎鎴愪竴缁勭浉浜?#37719;?#28003;?#37928;?#37832;嶅姟銆傛瘡涓?#37832;嶅姟璐?#29840;?#28051;缁勭壒瀹?#37510;?#37929;?#37711;?#37928;勫姛?#20824;銆傛瘡涓?#37832;嶅姟鍙?#28000;?#37832;?#38007;繁鐙珛鐨?#37825;版嵁搴?#38171;屼粠鑰?#28103;濊瘉涓?#37711;?#28000;?#37832;嶅姟瑙?#38000;?#37510;?/span>
寰?#37832;嶅姟?#32045;鐐?/span>
1銆?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">閫?#26473;囧垎瑙?#23480;?#28598;崟浣撳紡搴?#37922;?#28051;哄涓?#37832;嶅姟鏂规?#26337;鍐?#27996;嗗鏉傛棶棰?#38171;?/span>?#30241;涓?#23536;?#37832;嶅姟鐩?#28725;硅緝灏?/span>
2銆佹瘡涓崟浣?#25652;?#37922;?#28051;嶅眬闄?#27996;?#37733;?#28729;?#37928;勬妧鏈爤锛?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">寮?#24386;鑰?#37721;?#28000;?#38007;?#37922;?#38315;夋嫨寮?#24386;?#22951;鏈?#38171;屾?#24874;緵API鏈嶅姟銆?/span>
3銆?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">?#30241;涓?#23536;?#37832;嶅姟鐙珛鐨勫紑?#24386;锛?#38318;讲
4銆佸崟涓鑱?#29840;?#37716;熻兘锛?span style="font-family: Arial; background-color: #ffffff;">?#30241;涓?#37832;嶅姟閮?#23536;?#32480;?#23839;锛屽彧鍏?#23049;?#27996;?#28051;涓?#28051;氬姟鍔熻兘
5銆?span style="font-size: 16px; color: #454545; background-color: #ffffff;">鏄?#27996;?#29785;勬鍖?#23534;?#24386;锛屽涓?#23534;鍙戝洟闃?#37721;?#28000;苟琛屽紑?#24386;锛屾瘡涓?#37733;?#38339;?#29840;?#29840;?#28051;椤?#37832;嶅姟
6銆?#37824;瑰鏁?#38341;?#38341;旂銆備竴涓?#37832;嶅姟瀹?#37832;?#28051;嶄?#27692;?#21342;搷鍏?#28000;?#37928;?#37832;嶅姟
寰?#37832;嶅姟缂虹偣锛?/span>
1.寮?#24386;鑰呴渶瑕?#25652;斿?#29808;垱寤哄垎甯冨紡?#37108;缁熸墍浜?#37922;?#37928;勯澶?#37928;勫鏉傚洜绱?/span>
l  鐩?#37715;?#37928;処DE涓?#29781;?#38344;?#28725;?#37928;勬槸?#23839;浣?#23480;?#32459;嬬?#23338;簭锛?#37827;犳硶鏄?#32448;?#37824;?#37816;佸垎甯冨紡搴?#37922;?#37928;勫紑?#24386;
l  娴?#29831;曞伐浣?#37831;?#37716;?#37733;?#38341;?/span>
l  ?#28214;瑕?#38322;?#37922;?#37832;嶅姟?#26879;鐨?#38315;?#29825;?#37832;哄埗
l  寰?#38341;惧湪涓嶉噰鐢垎甯冨紡浜嬪姟鐨勬鍐?#28051;嬭法鏈嶅姟瀹炵幇鍔熻兘
l  ?#27861;鏈嶅姟瀹炵幇瑕佹眰鍔?#38003;借?#20345;?#20634;洟闃熶箣?#26879;鐨勭瀵?#37719;?#28003;?/span>
2.閮讲澶?#37833;?/span>
3.鍐?#28699;樺鐢?#38322;?#37831;?#26946;?/span>


abin 2017-12-31 16:41 ?#24386;琛?#29831;勮
]]>
JDK 婧?#37934;?#28051;?HashMap 鐨?hash 鏂规?#26334;?#29114;?#21976;槸浠?#31646;锛?/title><link>http://www.5432034.com/stevenjohn/archive/2017/12/24/432975.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sun, 24 Dec 2017 14:38:00 GMT</pubDate><guid>http://www.5432034.com/stevenjohn/archive/2017/12/24/432975.html</guid><wfw:comment>http://www.5432034.com/stevenjohn/comments/432975.html</wfw:comment><comments>http://www.5432034.com/stevenjohn/archive/2017/12/24/432975.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.5432034.com/stevenjohn/comments/commentRss/432975.html</wfw:commentRss><trackback:ping>http://www.5432034.com/stevenjohn/services/trackbacks/432975.html</trackback:ping><description><![CDATA[<div>JDK 鐨?HashMap 涓?#28003;?#37922;?#27996;?#28051;涓?hash 鏂规硶鏉仛 bit shifting锛屽湪娉?#38322;?#28051;?#29831;?#37828;庢槸涓?#27996;?#38339;叉涓?#31802;瀹炵?#29256;瘮杈冨樊鐨刪ashCode() 鏂规硶锛?#29831;烽棶鍘熺?#21976;槸浠?#31646;锛JDK 鐨勬簮鐮?#37721;?#29785;?#38171;GrepCode: java.util.HashMap (.java)</div><div>/**</div><div> * Applies a supplemental hash function to a given hashCode, which</div><div> * defends against poor quality hash functions.  This is critical</div><div> * because HashMap uses power-of-two length hash tables, that</div><div> * otherwise encounter collisions for hashCodes that do not differ</div><div> * in lower bits. Note: Null keys always map to hash 0, thus index 0.</div><div> */</div><div>static int hash(int h) {</div><div>    // This function ensures that hashCodes that differ only by</div><div>    // constant multiples at each bit position have a bounded</div><div>    // number of collisions (approximately 8 at default load factor).</div><div></div><div>    h ^= (h >>> 20) ^ (h >>> 12);</div><div>    return h ^ (h >>> 7) ^ (h >>> 4);</div><div>}</div><div></div><div>PS锛氱綉涓婄湅瑙?#37832;?#27996;?#29831;?#28003;?#38000;?#37832;?#27996;?#29831;村?#29114;悊?#28214;瑕?#37721;?#29785;?#37734;?#32513;?#37510;?#29825;?#32480;?#37832;虹?#23338;簭璁?#29825;?#38009;?#37832;?#37510;?#37928;?Vol.3 閲屽鐨勪粙缁?#38171;?#28051;?#26473;?#37832;?#37832;夌湅杩囩涔?#38171;屾眰杈?#27996;?#28000;?#32513;?/div><div><br /><br /><br /><br /><div> <br />杩欐浠?#37934;?#37721;?#8220;鎵?#37716;?#37713;?#37825;?#8221;銆?br />棰?#28051;?#29840;?#37928;勬槸Java 7鐨凥ashMap鐨勬簮鐮?#38171;Java 8涓?#26473;欐宸?#32513;?#32480;鍖?#27996;?#38171;屽彧?#20187;涓娆?6浣嶅彸浣嶇?#35826;?#20635;垨娣?#37722;?#38171;?#38000;?#28051;嶆槸鍥?#23046;?#38171;?#28003;嗗?#29114;?#21976;槸涓嶅彉鐨?#37510;備笅闈?#28000;Java 8鐨勬簮鐮?#28051;轰緥瑙?#38322;?#38171;?br /><br />//Java 8涓?#37928;?#37825;?#37714;?#37706;?#28028;樺寲鍑?#37825;皊taticfinalinthash(Objectkey){inth;return(key==null)?0:(h=key.hashCode())^(h>>>16);//key.hashCode()涓哄搱甯?#32480;楁硶锛?#26473;斿?#28850;?#28614;?#23338;搱甯屽紏<br />澶?#28729;?#38318;界煡閬?#28051;?#38344;?#28000;?#37934;?#38322;?#37928;刱ey.hashCode()鍑?#37825;拌皟鐢?#37928;勬槸key閿?#37706;?#32491;诲瀷鑷?#29999;?#37928;勫搱甯?#37713;?#37825;?#38171;?#26473;斿洖int?#28727;鏁?#37714;?#37706;?#37510;傜悊璁?#28051;?#37825;?#37714;?#37706;兼槸涓涓猧nt?#28727;锛?#28641;傛灉鐩存帴鎷?#37825;?#37714;?#37706;?#28003;?#28051;?#28051;嬫爣璁块棶HashMap涓?#37825;?#32513;?#37928;勮瘽锛?#38000;冭?#25117;埌2杩?#37714;?2浣嶅甫绗?#37721;?#37928;刬nt琛?#37706;艰?#20904;洿?#31904;-2147483648?#22476;2147483648銆?#37715;嶅悗鍔?#29863;?#37833;?#28598;?#22994;?0浜?#37928;勬槧灏勭?#27946;棿銆傚彧瑕?#37725;?#29999;?#37713;?#37825;版槧灏勫?#26945;瘮杈冨?#22247;寑鏉?#37825;?#38171;?#28051;鑸?#25652;?#37922;槸寰?#38341;?#37713;虹幇纰版挒鐨?#37510;備絾?#26870;棰樻槸涓涓?0浜块?#22446;害鐨?#37825;?#32513;?#38171;?#37712;?#28699;樻槸鏀?#28051;?#28051;?#37928;?#37510;備綘鎯?#38171;HashMap鎵?#28729;?#28052;嬪墠鐨?#37825;?#32513;勫?#28614;?#23338;灏?#37813;?6銆?#37813;浠?#26473;?#28051;?#37825;?#37714;?#37706;兼槸涓嶈兘鐩存帴鎷?#37833;?#37922;?#37928;?#37510;?#37922;?#28052;嬪墠杩樿鍏?#37707;氬鏁?#32513;?#37928;勯?#22446;害鍙栨杩?#32480;?#38171;屽?#26944;埌鐨?#28003;欐暟鎵嶈兘鐢?#37833;?#29825;块棶鏁?#32513;?#28051;嬫爣銆傛簮鐮?#28051;?#22959;?#26473;?#32480;楁槸?#28266;杩?#28051;猧ndexFor( )鍑?#37825;?#38322;?#28729;屾垚鐨?#37510;?br /><br />bucketIndex = indexFor(hash, table.length);indexFor鐨勪唬鐮佷篃寰?#32480;?#23839;锛?#28751;辨槸鎶?#37825;?#37714;?#37706;煎?#23678;暟缁勯?#22446;害?#20187;涓涓?涓?鎿?#28003;?#38171;?br /><br />static int indexFor(int h, int length) {        return h & (length-1);}椤轰究璇?#28051;涓?#38171;?#26473;?#28052;熸濂借閲?#27996;?#28051;?#28000;?#31646;HashMap鐨?#37825;?#32513;勯?#22446;害瑕?#37721;?鐨?#37825;?#23046;箓銆傚洜涓?#26473;欐牱锛?#37825;?#32513;勯?#22446;害-1锛夋濂?#37929;?#35120;?#27996;?#28051;涓?#8220;浣?#28003;嶆鐮?#8221;銆?#8220;涓?#8221;鎿?#28003;?#37928;?#32513;撴灉灏辨槸鏁?#37714;?#37706;?#37928;勯珮浣?#37711;?#38318;?#35120;掗浂锛屽彧淇濈暀浣?#28003;嶅?#38171;?#37922;?#37833;仛鏁?#32513;?#28051;嬫爣璁块棶銆備互鍒濆闀垮害16涓轰緥锛?6-1=15銆?杩?#37714;?#29723;?#32448;烘槸00000000 00000000 00001111銆傚?#23678;煇鏁?#37714;?#37706;煎仛“涓?#8221;鎿?#28003;?#28641;備笅锛?#32513;撴灉灏辨槸鎴?#37721;?#27996;?#37832;浣?#37928;勫洓浣嶅?#37510;?br />10100101 11000100 00100101& 00000000 00000000 00001111---------------------------------- 00000000 00000000 00000101    //楂?#28003;?#37711;?#38318;?#35120;掗浂锛屽彧淇濈暀鏈?#37733;?#28003;?br />浣?#26473;欐椂鍊欓棶棰?#28751;辨潵浜?#38171;?#26473;欐牱灏?#32480;楁垜鐨?#37825;?#37714;?#37706;煎垎甯冨啀鏉?#37825;?#38171;岃?#20345;槸鍙?#37721;?#37832;鍚?#37713;?#28003;?#37928;勮瘽锛岀?#29256;挒涔熶?#27692;緢涓?#38322;?#37510;傛洿瑕?#37723;?#37928;勬槸濡傛灉鏁?#37714;?#37832;韩鍋氬緱涓嶅锛屽垎甯?#28051;?#37812;愮?#22794;樊鏁板垪鐨勬紡?#30986;锛?#37805;板浣?#37832;鍚?#37713;?#28051;?#28003;?#28003;嶅?#22562;幇瑙勫緥鎬?#38322;嶅锛?#28751;?#37827;犳瘮嬬銆?#26473;欐椂鍊?#8220;鎵?#37716;?#37713;?#37825;?#8221;鐨勪?#23792;?#28751;?#28003;撶幇鍑?#37833;?#27996;?#38171;?#29831;村埌杩欓?#23677;瀹?#25652;?#29831;?#37914;?#37713;?#37833;?#27996;?#37510;傜湅涓?#38344;?#26473;?#28051;?#37733;?#38171;?br /><div style="position: absolute; width: 104px; height: 104px;"></div><img src="http://www.5432034.com/images/blogjava_net/stevenjohn/4acf898694b8fb53498542dc0c5f765a_hd.jpg" border="0" alt="" /><br /><br />鍙?#28003;嶇16浣?#38171;屾濂芥槸32bit鐨?#28051;鍗?#38171;?#38007;繁鐨勯?#27194;?#23106;?#21700;拰浣?#37719;婂?#21700;?#27692;?#20635;垨锛?#28751;辨槸涓?#27996;嗘؜鍚?#37720;熷?#23338;搱甯?#37934;?#37928;勯珮浣嶅拰浣?#28003;?#38171;?#28000;?#23005;?#37833;?#37716;?#28598;?#28003;?#28003;?#37928;勯殢鏈?#37804;?#37510;?#38000;?#28051;?#23075;?#37722;?#37722;?#37928;?#28003;?#28003;嶆鏉備簡楂?#28003;?#37928;?#38318;垎鐗瑰緛锛?#26473;欐牱楂?#28003;?#37928;勪俊鎭?#28052;熻鍙?#37929;镐繚?#26240;涓?#37833;?#37510;?#37832;鍚庢垜浠?#37833;湅涓涓婸eterLawley鐨?#28051;绡?#28051;撴?#24532;枃绔?#37510;夾n introduction to optimising a hashing strategy銆?#38322;?#37928;?#37928;?#28051;涓?#28729;?#26976;?#38171;?#28000;?#38341;?#37832;?#38315;?#37721;?#27996;?52涓?#28699;?#32471;?#28051;?#38171;屽湪浠?#28000;?#37825;?#37714;?#37706;?#28729;?#37711;?#23052;?#37832;?#37712;?#32464;?#37928;勫墠鎻愪笅锛屽瀹?#28000;仛浣?#28003;嶆鐮?#38171;屽彇鏁?#32513;?#28051;嬫爣銆?br /><br /><img src="http://www.5432034.com/images/blogjava_net/stevenjohn/22222.jpg" border="0" alt="" /><br />缁撴灉鏄?#32448;?#38171;?#35120;HashMap鏁?#32513;勯?#22446;害涓?12鐨?#37827;?#37706;?#38171;屼篃灏辨槸鐢?#37818;?#37934;?#37721;?#28003;?浣?#37928;?#37827;?#37706;?#38171;屽湪娌?#37832;?#37813;?#37716;?#37713;?#37825;?#37928;勬鍐?#28051;?#38171;屽彂鐢?#27996;?03娆?#32432;版挒锛屾帴杩?0%銆?#38000;屽湪浣?#37922;?#27996;?#37813;?#37716;?#37713;?#37825;?#28052;嬪悗鍙?#37832;?2娆?#32432;版挒銆傜?#29256;?#28850;噺灏?#27996;?#28751;?#26473;?0%銆傜湅鏉?#37813;?#37716;?#37713;?#37825;扮瀹?#26473;樻槸鏈?#37716;熸晥鐨?#37510;備?#21976;槑鏄Java 8瑙?#23536;?#37813;?#37716;仛涓娆?#28751;卞浜?#38171;屽仛4娆?#37928;勮瘽锛屽浜嗗彲鑳借闄?#37825;?#37922;?#28052;?#28051;嶅锛屾墍璋?#28051;?#27996;?#37825;?#37916;?#38000;冭檻灏?#37824;?#37812;愪竴娆?#27996;?#37510;?br />------------------------------------------------------</div><br /><br /><br /><br /><br /><br /><br /><br /><div>https://www.zhihu.com/question/20733617</div><br /><br /><br /></div><img src ="http://www.5432034.com/stevenjohn/aggbug/432975.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.5432034.com/stevenjohn/" target="_blank">abin</a> 2017-12-24 22:38 <a href="http://www.5432034.com/stevenjohn/archive/2017/12/24/432975.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>GoLang?#31651;鏂规硶涓庢帴鍙?/title><link>http://www.5432034.com/stevenjohn/archive/2017/08/03/432720.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Thu, 03 Aug 2017 03:34:00 GMT</pubDate><guid>http://www.5432034.com/stevenjohn/archive/2017/08/03/432720.html</guid><wfw:comment>http://www.5432034.com/stevenjohn/comments/432720.html</wfw:comment><comments>http://www.5432034.com/stevenjohn/archive/2017/08/03/432720.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.5432034.com/stevenjohn/comments/commentRss/432720.html</wfw:commentRss><trackback:ping>http://www.5432034.com/stevenjohn/services/trackbacks/432720.html</trackback:ping><description><![CDATA[<p>Go璇?#29815;娌?#37832;?#23052;胯浼?#32513;?#38344;?#37722;戝?#30789;薄缂栫涓?#37928;勮澶氭?#20634;康锛屾瘮濡?#32513;?#37813;?#37510;佽?#27692;嚱鏁?#37510;佹瀯閫?#37713;?#37825;板?#23678;鏋勫嚱鏁?#37510;?#38341;?#38040;?#37928;則his鎸?#38333;堢瓑銆?/p> <p> </p> <h2>鏂规硶</h2> <p><span style="line-height: 1.5;">Go 璇?#29815;涓?#37722;?#37827;?#37832;?#37713;?#37825;板?#23678;?#35268;硶銆?span style="color: #ff0000;">鏂规硶灏辨槸涓涓?#37718;?#37722;?#27996;嗘帴鍙?#38000;?#38171;receiver锛?#37928;勫嚱鏁?/span>锛宺eceiver鍙?#28000;槸鍐?#32515;?#32491;诲瀷鎴?#38000;?#32513;撴瀯浣?#32491;诲瀷鐨?#28051;涓?#37706;?#37812;?#38000;呮槸涓涓?#37816;?#38333;?#37510;?#37813;鏈?#32513;?#28729;氱?#35826;瀷鐨?#37826;规?#26334;睘浜?#29831;?#32491;诲瀷鐨?#37826;规硶闆?#37510;?br /></span></p> <p>濡備笅闈?#37928;?#26473;?#28051;?#28186;嬪瓙锛?#28729;氫箟浜?#28051;涓?#37826;?#32491;诲瀷Integer锛?#28729;冨拰int涓鏍?#38171;屽彧?#27128;涓?#28729;冨唴缃?#37928;刬nt绫诲?#23338;鍔?#27996;?#28051;?#37826;版?#35268;硶Less()</p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>type Integer <span style="color: #0000ff;">int</span> func (a Integer) Less(b Integer) bool { return a < b } func main() { var a Integer = 1 <span style="color: #0000ff;">if</span> a.Less(2) { fmt.Println("less then 2") } }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p>鍙?#28000;?#37930;嬪嚭锛孏o璇?#29815;?#28266;鑷?#28729;氫箟绫诲瀷鐨勫?#30789;薄涓?#23052;?#37832;C++/Java閭?#32457;嶉殣钘?#37928;則his鎸?#38333;?#38171;?#38000;屾槸?#28266;瀹氫箟鎴愬憳鏂规硶鏃舵?#24807;紡澹版槑浜?#37711;舵墍灞?#37928;勫?#30789;薄銆?/p> <p> </p> <p>method鐨勮?#30838;濡備笅锛?/p> <div> <pre>func (r ReceiverType) funcName(parameters) (results)</pre> </div> <p>褰?#29835;?#37922;method鏃?#38171;屼細灏唕eceiver浣?#28051;哄嚱鏁?#37928;勭涓涓?#37721;傛暟锛?/p> <div> <pre>funcName(r, parameters);</pre> </div> <p>鎵浠?#38171;宺eceiver?#27128;鍊?#32491;诲瀷杩樻槸鎸?#38333;?#32491;诲瀷瑕佺湅method鐨?#28003;?#37922;?#37510;?#28641;傛灉瑕?#28103;?#37824;瑰?#30789;薄鐨勫?#38171;?#28751;遍渶瑕佷紶閫掑?#30789;薄鐨?#37816;?#38333;?#37510;?/p> <p>鎸?#38333;?#28003;?#28051;篟eceiver浼氬瀹炰?#23338;?#30789;薄鐨勫唴瀹瑰彂鐢熸搷浣?鑰屾櫘閫氱?#35826;瀷浣?#28051;篟eceiver浠?#28000;呮槸浠?#37715;?#37832;?#28003;?#28051;烘搷浣?#28725;硅薄,?#33503;涓嶅?#29808;師瀹炰?#23338;?#30789;薄?#24386;鐢熸搷浣?#37510;?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>func (a *Ingeger) Add(b Integer) { *a += b } func main() { var a Integer = 1 a.Add(3) fmt.Println("a =", a) // a = 4 }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p>濡傛灉Add鏂规硶涓?#28003;?#37922;?#37816;?#38333;?#38171;屽?#27267;杩斿洖鐨?#32513;撴灉涓嶅彉锛?#26473;欐槸鍥?#28051;Go璇?#29815;鍑?#37825;?#37928;勫?#20635;暟涔熸槸?#29056;浜?#37706;?#28028;?#38315;?#37510;?/p> <p>娉剰锛?span style="color: #ff0000;">褰撴?#35268;硶鐨勬帴鍙?#38000;呮槸鎸?#38333;?#37827;?#38171;?#37719;?#28003;?#37922;?#37706;?#32491;诲瀷璋?#37922;?#38317;箞鏂规硶鍐?#38318;?#28052;熸槸瀵?#37816;?#38333;?#37928;勬搷浣?#37510;?/span></p> <p> </p> <p>涔嬪墠璇?#26473;?#38171;孏o璇?#29815;娌?#37832;?#37835;?#38315;?#37713;?#37825;?#37928;勬?#20634;康锛?#38315;?#29999;?#28003;?#37922;?#28051;涓?#37711;?#28766;鍑?#37825;版潵瀹屾垚銆備緥濡?#38171;?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>func NewRect(x, y, width, height float64) *Rect { return &Rect{x, y, width, height} } func main() { rect1 := NewRect(1,2,10,20) fmt.Println(rect1.width) }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <p> </p> <hr /> <h3><span style="font-size: 14px; line-height: 1.5;">鍖垮悕缁勫悎</span></h3> <p><span style="line-height: 1.5;">Go璇?#29815;鎻愪緵浜?#32513;?#37813;?#38171;?#28003;嗘槸閲?#37922;?#27996;?#32513;勫悎鐨勮?#30838;锛屾垜浠?#28751;?#37711;?#32457;?#28051;哄?#22446;悕缁勫悎锛屼緥濡?#38171;?/span></p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>type Base struct { name string } func (base *Base) Set(myname string) { base.name = myname } func (base *Base) Get() string { return base.name } type Derived struct { Base age <span style="color: #0000ff;">int</span> } func (derived *Derived) Get() (nm string, ag <span style="color: #0000ff;">int</span>) { return derived.name, derived.age } func main() { b := &Derived{} b.Set("sina") fmt.Println(b.Get()) }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p>渚嬪?#24874;腑锛屽湪Base绫诲?#23338;?#27691;箟浜唃et()?#25328;set()涓?#28051;?#37826;规硶锛?#38000;孌erived绫诲瀷缁?#37813;?#27996;咮ase绫?#38171;屽苟鏀?#37712;?#27996;Get()鏂规硶锛屽湪Derived瀵硅薄璋?#37922;Set()鏂规硶锛屼?#27692;姞杞藉?#34425;?#35826;搴?#37928;?#37826;规硶锛?#38000;岃皟鐢Get()鏂规硶鏃?#38171;?#37716;犺?#33445;鐢?#32491;?#37824;?#37712;?#37928;?#37826;规硶銆?/p> <p> </p> <p>缁勫悎鐨?#32491;诲?#23338;?#23683;缁勫悎鐨?#32491;诲?#23338;寘鍚?#37722;?#37722;?#37812;愬憳鏃?#38171;?nbsp;?#32048;涓嶄細鏈夐棶棰樺憿锛?#37721;?#28000;?#37721;?#38000;?#28051;?#38344;?#37928;勪?#23338;瓙锛?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>type Base struct { name string age <span style="color: #0000ff;">int</span> } func (base *Base) Set(myname string, myage <span style="color: #0000ff;">int</span>) { base.name = myname base.age = myage } type Derived struct { Base name string } func main() { b := &Derived{} b.Set("sina", 30) fmt.Println("b.name =",b.name, "\tb.Base.name =", b.Base.name) fmt.Println("b.age =",b.age, "\tb.Base.age =", b.Base.age) }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <p> </p> <p> </p> <hr /> <h2>鍊?#29831;?#28052;夊?#23677;紩鐢?#29831;?#28052;?/h2> <p>鍊?#29831;?#28052;夊?#23677;紩鐢?#29831;?#28052;?#37928;勫樊鍒湪浜?#29863;嬪?#38171;屾瘮濡?/p> <div> <pre>b = a b.Modify()</pre> </div> <p><span style="line-height: 1.5;">濡傛灉b鐨勪慨鏀?#28051;嶄?#27692;?#21342;搷a鐨勫?#38171;?#38317;箞姝?#32491;诲?#23338;睘浜?#37706;?#32491;诲瀷锛?#28641;傛灉浼氬?#21342;搷a鐨勫?#38171;?#38317;箞姝?#32491;诲瀷?#27128;寮?#37922;?#32491;诲瀷銆?/span></p> <p>Go璇?#29815;涓?#37928;勫澶?#37825;?#32491;诲瀷閮藉熀浜?#37706;?#29831;?#28052;?#38171;屽?#21614;嫭锛?/p> <ul> <li>?#29056;鏈?#32491;诲瀷锛?#28641;byte銆乮nt銆乥ool銆乫loat32銆乻tring?#29905;锛?/li> <li><span style="line-height: 1.5;">澶嶅悎绫诲瀷锛?#28641;arry銆乻truct銆乸ointer?#29905;锛?/span></li> </ul> <p> </p> <p>C璇?#29815;涓?#37928;?#37825;?#32513;勬瘮?#32221;鐗瑰埆锛?#38315;?#26473;囧嚱鏁?#28028;?#38315;?#28051;涓?#37825;?#32513;?#37928;?#37827;?#37706;欏熀浜?#23534;?#37922;?#29831;?#28052;?#38171;?#28003;嗘槸?#28266;缁撴瀯浣?#28729;氫箟鏁?#32513;勫彉閲?#37928;?#37827;?#37706;欏熀浜?#37706;?#29831;?#28052;?#37510;?#38000;屽湪Go璇?#29815;涓?#38171;屾暟缁勫?#23677;熀鏈?#32491;诲瀷娌?#37832;夊?#21700;埆锛屾槸寰堢函?#34917;鐨勫?#32491;诲瀷锛屼緥濡?#38171;?/p> <div> <pre>var a = [3] <span style="color: #0000ff;">int</span>{1,2,3} var b = a b[1]++ fmt.Println(a, b) // [1 2 3] [1 3 3]</pre> </div> <p>?#31904;缁撴灉?#28229;锛宐=a璧嬪?#29831;?#37721;槸鏁?#32513;勫唴瀹?#37928;勫?#23678;暣澶嶅埗锛岃?#20345;兂琛?#26440;惧紩鐢?#38171;岄渶瑕?#37922;?#37816;?#38333;?#38171;?/p> <div> <pre>var a = [3] <span style="color: #0000ff;">int</span>{1,2,3} var b = &a銆銆銆銆// 寮?#37922;?#29831;?#28052;?b[1]++ fmt.Println(a, b) // [1 3 3] [1 3 3]</pre> </div> <p> </p> <p> </p> <hr /> <h2>?#24116;鍙?/h2> <p>Interface ?#27128;涓缁勬?#20511;薄鏂规硶锛?#37832;?#37711;?#28003;?#28729;炵幇鐨?#37826;规硶/浠呭寘鍚?#37826;规硶鍚嶅?#20635;暟杩斿?#28850;?#37928;?#37826;规硶锛?#37928;勯泦鍚?#38171;?#28641;傛灉瀹炵幇浜?interface 涓?#37928;?#37813;鏈?#37826;规硶锛?#37719;?#29831;?#32491;?瀵硅薄灏?#28729;炵幇浜?#29831;帴鍙?#37510;?/p> <p>Interface 鐨勫?#29256;?#24226;?#29006;紡锛?/p> <div> <pre>type interfaceName interface { //鏂规?#26334;垪琛? } </pre> </div> <p>Interface 鍙?#28000;?#29722;?#28000;绘?#24531;?#30789;薄瀹炵幇锛?#28051;涓?#32491;诲瀷/瀵硅薄涔?#37721;?#28000;?#28729;炵幇澶?#28051;?interface锛?br /><span style="background-color: #ffff00;">interface鐨勫彉閲忓彲浠?#37816;?#37832;?#28000;绘剰瀹炵幇璇interface绫诲瀷鐨勫?#30789;薄銆?/span></p> <p> 濡備笅闈?#37928;勪?#23338;瓙锛?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>package main import <span style="color: #800000;">"</span><span style="color: #800000;">fmt</span><span style="color: #800000;">"</span> type Human <span style="color: #0000ff;">struct</span> { name <span style="color: #0000ff;">string</span> age <span style="color: #0000ff;">int</span> phone <span style="color: #0000ff;">string</span> } type Student <span style="color: #0000ff;">struct</span> { Human <span style="color: #008000;">//</span><span style="color: #008000;">鍖垮?#23941;?#26945;</span> school <span style="color: #0000ff;">string</span> loan float32 } type Employee <span style="color: #0000ff;">struct</span> { Human <span style="color: #008000;">//</span><span style="color: #008000;">鍖垮?#23941;?#26945;</span> company <span style="color: #0000ff;">string</span> money float32 } <span style="color: #008000;">//</span><span style="color: #008000;">Human瀹炵幇SayHi鏂规硶</span> func (h Human) SayHi() { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">Hi, I am %s you can call me on %s\n</span><span style="color: #800000;">"</span>, h.name, h.phone) } <span style="color: #008000;">//</span><span style="color: #008000;">Human瀹炵幇Sing鏂规硶</span> func (h Human) Sing(lyrics <span style="color: #0000ff;">string</span>) { fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">La la la la...</span><span style="color: #800000;">"</span>, lyrics) } <span style="color: #008000;">//</span><span style="color: #008000;">Employee閲嶈浇Human鐨凷ayHi鏂规硶</span> func (e Employee) SayHi() { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">Hi, I am %s, I work at %s. Call me on %s\n</span><span style="color: #800000;">"</span>, e.name, e.company, e.phone) } <span style="color: #008000;">//</span><span style="color: #008000;"> Interface Men琚獺uman,Student?#25328;Employee瀹炵幇 </span><span style="color: #008000;">//</span><span style="color: #008000;"> 鍥?#28051;?#26473;?#28051;?#28051;?#32491;诲瀷閮?#28729;炵幇浜?#26473;?#28051;?#28051;?#37826;规硶</span> type Men <span style="color: #0000ff;">interface</span> { SayHi() Sing(lyrics <span style="color: #0000ff;">string</span>) } func main() { mike := Student{Human{<span style="color: #800000;">"</span><span style="color: #800000;">Mike</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>, <span style="color: #800000;">"</span><span style="color: #800000;">222-222-XXX</span><span style="color: #800000;">"</span>}, <span style="color: #800000;">"</span><span style="color: #800000;">MIT</span><span style="color: #800000;">"</span>, <span style="color: #800080;">0.00</span>} paul := Student{Human{<span style="color: #800000;">"</span><span style="color: #800000;">Paul</span><span style="color: #800000;">"</span>, <span style="color: #800080;">26</span>, <span style="color: #800000;">"</span><span style="color: #800000;">111-222-XXX</span><span style="color: #800000;">"</span>}, <span style="color: #800000;">"</span><span style="color: #800000;">Harvard</span><span style="color: #800000;">"</span>, <span style="color: #800080;">100</span>} sam := Employee{Human{<span style="color: #800000;">"</span><span style="color: #800000;">Sam</span><span style="color: #800000;">"</span>, <span style="color: #800080;">36</span>, <span style="color: #800000;">"</span><span style="color: #800000;">444-222-XXX</span><span style="color: #800000;">"</span>}, <span style="color: #800000;">"</span><span style="color: #800000;">Golang Inc.</span><span style="color: #800000;">"</span>, <span style="color: #800080;">1000</span>} tom := Employee{Human{<span style="color: #800000;">"</span><span style="color: #800000;">Tom</span><span style="color: #800000;">"</span>, <span style="color: #800080;">37</span>, <span style="color: #800000;">"</span><span style="color: #800000;">222-444-XXX</span><span style="color: #800000;">"</span>}, <span style="color: #800000;">"</span><span style="color: #800000;">Things Ltd.</span><span style="color: #800000;">"</span>, <span style="color: #800080;">5000</span>} <span style="color: #008000;">//</span><span style="color: #008000;">瀹氫箟Men绫诲瀷鐨勫彉閲廼</span> <span style="color: #0000ff;">var</span> i Men <span style="color: #008000;">//</span><span style="color: #008000;">i?#20824;瀛樺偍Student</span> i = mike銆銆銆銆 fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">This is Mike, a Student:</span><span style="color: #800000;">"</span>) i.SayHi() i.Sing(<span style="color: #800000;">"</span><span style="color: #800000;">November rain</span><span style="color: #800000;">"</span>) <span style="color: #008000;">//</span><span style="color: #008000;">i涔熻兘瀛樺偍Employee</span> i = tom fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">This is tom, an Employee:</span><span style="color: #800000;">"</span>) i.SayHi() i.Sing(<span style="color: #800000;">"</span><span style="color: #800000;">Born to be wild</span><span style="color: #800000;">"</span>) <span style="color: #008000;">//</span><span style="color: #008000;">瀹氫箟浜唖lice Men</span> fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">Let's use a slice of Men and see what happens</span><span style="color: #800000;">"</span>) x := make([]Men, <span style="color: #800080;">3</span>) <span style="color: #008000;">//</span><span style="color: #008000;">杩?#28051;?#28051;?#38318;芥槸涓嶅悓绫诲瀷鐨勫厓绱?#38171;?#28003;嗘槸浠?#28000;?#28729;炵幇浜interface鍚?#28051;涓帴鍙?/span> x[<span style="color: #800080;">0</span>], x[<span style="color: #800080;">1</span>], x[<span style="color: #800080;">2</span>] = paul, sam, mike <span style="color: #0000ff;">for</span> _, value := range x{ value.SayHi() } }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <h3>绌烘帴鍙?/h3> <p>绌篿nterface(interface{})涓嶅寘鍚?#28000;讳綍鐨刴ethod锛屾鍥?#28051;?#28641;傛锛?span style="background-color: #ffff00;">鎵鏈?#37928;?#32491;诲瀷閮?#28729;炵幇浜?#32460;篿nterface</span>銆傜?#31743;nterface瀵?#27996;?#37819;?#26473;拌v涓嶅埌浠讳綍鐨?#28003;?#37922;?鍥?#28051;?#28729;?#28051;嶅寘鍚?#28000;讳綍鐨刴ethod锛?#38171;?#28003;嗘槸<span style="background-color: #ffff00;">绌篿nterface?#28266;?#22428;浠渶瑕?#28699;樺偍浠绘剰绫诲瀷鐨?#37825;板?#37928;?#37827;?#37706;?#37929;?#35120;?#37832;?#37922;?#38171;屽洜涓?#28729;冨彲浠?#28699;樺偍浠绘剰绫诲瀷鐨?#37825;板?#37510;?#28729;?#37832;?#37904;?#32491;讳技浜嶤璇?#29815;鐨剉oid*绫诲瀷銆?/span></p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre><span style="color: #008000;">//</span><span style="color: #008000;"> 瀹氫箟a涓虹?#28888;帴鍙?/span> <span style="color: #0000ff;">var</span> a <span style="color: #0000ff;">interface</span>{} <span style="color: #0000ff;">var</span> i <span style="color: #0000ff;">int</span> = <span style="color: #800080;">5</span> s := <span style="color: #800000;">"</span><span style="color: #800000;">Hello world</span><span style="color: #800000;">"</span> <span style="color: #008000;">//</span><span style="color: #008000;"> a鍙?#28000;?#28699;樺偍浠绘剰绫诲瀷鐨?#37825;板?/span> a = i a = s</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <p>interface鐨勫彉閲?#38322;?#38344;?#37721;?#28000;?#28699;樺偍浠绘剰绫诲瀷鐨?#37825;板?#38171;?#29831;?#32491;诲?#23338;?#28853;幇浜interface锛?#38171;?#38317;箞?#22428;浠?#37804;庝箞鍙嶅?#25120;煡閬?#26473;?#28051;猧nterface鍙?#38322;?#38322;?#38344;?#28729;?#38340;?#28103;?#28699;?#27996;?#37928;勬槸鍝?#28051;?#32491;诲瀷鐨勫?#30789;薄鍛?#38171;?#37929;?#37715;嶅父鐢?#37928;?#37832;?#28051;?#32457;?#37826;规硶锛歴witch娴?#29831;?#37510;丆omma-ok鏂?#29815;銆?/p> <p> </p> <p>switch娴?#29831;?#28641;備笅锛?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>type Element <span style="color: #0000ff;">interface</span>{} type List [] Element type Person <span style="color: #0000ff;">struct</span> { name <span style="color: #0000ff;">string</span> age <span style="color: #0000ff;">int</span> } <span style="color: #008000;">//</span><span style="color: #008000;">鎵?#37719;?/span> func (p Person) String() <span style="color: #0000ff;">string</span> { <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">(name: </span><span style="color: #800000;">"</span> + p.name + <span style="color: #800000;">"</span><span style="color: #800000;"> - age: </span><span style="color: #800000;">"</span>+strconv.Itoa(p.age)+ <span style="color: #800000;">"</span><span style="color: #800000;"> years)</span><span style="color: #800000;">"</span> } func main() { list := make(List, <span style="color: #800080;">3</span>) list[<span style="color: #800080;">0</span>] = <span style="color: #800080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;">an int </span> list[<span style="color: #800080;">1</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">Hello</span><span style="color: #800000;">"</span> <span style="color: #008000;">//</span><span style="color: #008000;">a string</span> list[<span style="color: #800080;">2</span>] = Person{<span style="color: #800000;">"</span><span style="color: #800000;">Dennis</span><span style="color: #800000;">"</span>, <span style="color: #800080;">70</span>} <span style="color: #0000ff;">for</span> index, element := range list{ <span style="color: #0000ff;">switch</span> value := element.(type) { <span style="color: #0000ff;">case</span> <span style="color: #0000ff;">int</span>: fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is an int and its value is %d\n</span><span style="color: #800000;">"</span>, index, value) <span style="color: #0000ff;">case</span> <span style="color: #0000ff;">string</span>: fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is a string and its value is %s\n</span><span style="color: #800000;">"</span>, index, value) <span style="color: #0000ff;">case</span> Person: fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is a Person and its value is %s\n</span><span style="color: #800000;">"</span>, index, value) <span style="color: #0000ff;">default</span>: fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is of a different type</span><span style="color: #800000;">"</span>, index) } } }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <p>濡傛灉浣?#37922;Comma-ok鏂?#29815;鐨勮瘽锛?/p> <div><div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div> <pre>func main() { list := make(List, <span style="color: #800080;">3</span>) list[<span style="color: #800080;">0</span>] = <span style="color: #800080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;"> an int</span> list[<span style="color: #800080;">1</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">Hello</span><span style="color: #800000;">"</span> <span style="color: #008000;">//</span><span style="color: #008000;"> a string</span> list[<span style="color: #800080;">2</span>] = Person{<span style="color: #800000;">"</span><span style="color: #800000;">Dennis</span><span style="color: #800000;">"</span>, <span style="color: #800080;">70</span>} <span style="color: #0000ff;">for</span> index, element := range list { <span style="color: #0000ff;">if</span> value, ok := element.(<span style="color: #0000ff;">int</span>); ok { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is an int and its value is %d\n</span><span style="color: #800000;">"</span>, index, value) } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> value, ok := element.(<span style="color: #0000ff;">string</span>); ok { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is a string and its value is %s\n</span><span style="color: #800000;">"</span>, index, value) } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> value, ok := element.(Person); ok { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is a Person and its value is %s\n</span><span style="color: #800000;">"</span>, index, value) } <span style="color: #0000ff;">else</span> { fmt.Printf(<span style="color: #800000;">"</span><span style="color: #800000;">list[%d] is of a different type\n</span><span style="color: #800000;">"</span>, index) } } }</pre> <div><img src="http://common.cnblogs.com/images/copycode.gif" alt="澶嶅埗浠?#37934;? /></div></div> <p> </p> <p> </p> <h3>宓?#37711;帴鍙?/h3> <p>姝?#28641;struct绫诲?#23338;彲浠?#37718;?#37722;?#28051;涓?#37718;垮?#23941;?#26945;锛宨nterface涔?#37721;?#28000;?#23443;屽鍙?#28598;?#28051;涓帴鍙?#37510;?/p> <p>濡傛灉涓涓猧nterface1浣?#28051;篿nterface2鐨?#28051;涓?#23443;?#37711;?#28699;楁锛?#38317;箞interface2闅愬紡鐨勫寘鍚?#27996;interface1閲?#38344;?#37928;刴ethod銆?/p> <p> </p> <p> </p> <h3>鍙?#28751;?/h3> <p>鎵璋?#37721;?#28751;?#38171;reflect锛?#28751;辨槸?#20824;妫鏌?#32459;嬪?#24531;湪杩?#29723;?#37827;?#37928;勭?#33333;?#37510;?/p> <p>浣?#37922;reflect涓鑸垎鎴愪?#22795;锛?#28051;?#38344;?#32480;瑕?#37928;勮瑙?#28051;涓?#38171;氳?#20344;?#35826;弽灏勬槸涓涓?#32491;诲瀷鐨勫?杩欎簺鍊?#38318;?#28729;炵幇浜?#32460;篿nterface)锛岄鍏堥渶瑕佹妸瀹冭浆鍖?#37812;reflect瀵硅薄(reflect.Type鎴?#38000;卹eflect.Value锛屾?#35268;嵁涓嶅悓鐨勬鍐?#29835;?#37922;?#28051;嶅悓鐨勫嚱鏁?銆?#26473;?#28051;?#32457;嶈?#23792;彇鏂瑰紡濡備笅锛?/p> <div> <pre> t := reflect.TypeOf(i) <span style="color: #008000;">//</span><span style="color: #008000;">寰楀埌绫诲瀷鐨勫?#20905;?#29256;嵁,閫?#26473;t?#22428;浠?#38003;借?#23792;彇绫诲?#23338;?#27691;箟閲?#38344;?#37928;?#37813;鏈?#37711;?#32497;?/span> v := reflect.ValueOf(i) <span style="color: #008000;">//</span><span style="color: #008000;">寰楀埌瀹?#38340;?#37928;勫?#38171;?#38315;?#26473;v?#22428;浠?#38014;峰彇瀛樺偍?#28266;閲?#38344;?#37928;勫?#38171;?#26473;樺彲浠?#37720;?#37824;瑰?#27194;?/span></pre> </div> <p> </p> <p>?#27974;鍖?#28051;簉eflect瀵硅薄涔嬪悗?#22428;浠?#28751;卞彲浠?#26473;?#29723;?#28051;?#31802;鎿?#28003;?#27996;?#38171;屼篃灏辨槸灏唕eflect瀵硅薄?#27974;鍖?#37812;?#37929;?#25652;?#37928;勫?#38171;屼緥濡?/p> <div> <pre>tag := t.Elem().Field(<span style="color: #800080;">0</span>).Tag <span style="color: #008000;">//</span><span style="color: #008000;">鑾峰彇瀹氫?#22794;湪struct閲?#38344;?#37928;勬?#22249;</span> name := v.Elem().Field(<span style="color: #800080;">0</span>).String() <span style="color: #008000;">//</span><span style="color: #008000;">鑾峰彇瀛樺偍?#28266;绗?#28051;涓?#28699;楁閲?#38344;?#37928;勫?/span></pre> </div> <p> </p> <p>鑾峰彇鍙?#28751;勫艰兘杩斿洖鐩?#25652;?#37928;?#32491;诲?#23338;?#23678;?#26495;?/p> <div> <pre><span style="color: #0000ff;">var</span> x float64 = <span style="color: #800080;">3.4</span> v := reflect.ValueOf(x) fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">type:</span><span style="color: #800000;">"</span>, v.Type()) fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">kind is float64:</span><span style="color: #800000;">"</span>, v.Kind() == reflect.Float64) fmt.Println(<span style="color: #800000;">"</span><span style="color: #800000;">value:</span><span style="color: #800000;">"</span>, v.Float())</pre> </div> <p> </p> <p>鏈鍚?#38171;屽弽灏?#37928;勮瘽锛?#38317;箞鍙?#28751;?#37928;勫?#26945;?#38747;繀椤绘槸鍙?#28103;?#37824;?#37928;?#38171;屾垜浠?#37715;?#38344;?#28699;?#28052;?#26473;囦紶鍊煎?#23676;紶寮?#37922;?#38171;?#26473;?#28051;?#38322;?#38344;?#28052;熸槸涓鏍?#37928;勯?#25782;悊銆傚弽灏?#37928;勫?#26945;?#38747;繀椤绘槸鍙?#29831;?#37712;?#37928;勬剰鎬濇槸锛?#28641;傛灉涓?#38344;?#26473;欐?#23792;啓锛?#38317;箞浼氬彂鐢熼敊璇?/p> <div> <pre><span style="color: #0000ff;">var</span> x float64 = <span style="color: #800080;">3.4</span> v := reflect.ValueOf(x) v.SetFloat(<span style="color: #800080;">7.1</span>)</pre> </div> <p> </p> <p>濡傛灉瑕?#28103;?#37824;?#37929;?#25652;?#37928;勫?#38171;屽繀椤?#26473;欐?#23792;啓</p> <div> <pre><span style="color: #0000ff;">var</span> x float64 = <span style="color: #800080;">3.4</span> p := reflect.ValueOf(&x) v := p.Elem() v.SetFloat(<span style="color: #800080;">7.1</span>)</pre> </div> <p>涓?#38344;?#37721;槸瀵瑰弽灏?#37928;勭畝?#23839;浠?#32513;?#38171;屾洿?#32321;鍏?#37928;勭悊瑙?#26473;橀渶瑕?#38007;?#23480;卞湪缂栫涓?#28051;?#37826;?#37928;勫疄璺?#37510;?/p> <p> </p> <p> </p> <p><strong>鍙?#38000;冩枃妗?#38171;?/strong></p> <p><a href="http://se77en.cc/2014/05/05/methods-interfaces-and-embedded-types-in-golang/" target="_blank">http://se77en.cc/2014/05/05/methods-interfaces-and-embedded-types-in-golang/</a></p> <p><a href="http://se77en.cc/2014/05/04/choose-whether-to-use-a-value-or-pointer-receiver-on-methods/" target="_blank">http://se77en.cc/2014/05/04/choose-whether-to-use-a-value-or-pointer-receiver-on-methods/<br /></a></p> <p> http://www.cnblogs.com/chenny7/p/4497969.html<br /><br /><br /><br /></p><p><br /><br /></p><img src ="http://www.5432034.com/stevenjohn/aggbug/432720.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.5432034.com/stevenjohn/" target="_blank">abin</a> 2017-08-03 11:34 <a href="http://www.5432034.com/stevenjohn/archive/2017/08/03/432720.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>鑰佽櫈瑕?#28699;GoLang-鍑?#37825;?涓?http://www.5432034.com/stevenjohn/archive/2017/08/02/432718.htmlabinabinWed, 02 Aug 2017 08:39:00 GMThttp://www.5432034.com/stevenjohn/archive/2017/08/02/432718.htmlhttp://www.5432034.com/stevenjohn/comments/432718.htmlhttp://www.5432034.com/stevenjohn/archive/2017/08/02/432718.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/432718.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/432718.html涓嶅彲鎴栫己鐨勫嚱鏁?#38171;屽湪Go涓?#28729;氫箟鍑?#37825;?#37928;?#37826;瑰紡濡備笅锛?/p>
func (p myType ) funcName ( a, b int , c string ) ( r , s int ) {     return } 

閫?#26473;囧嚱鏁?#28729;氫箟锛屾垜浠?#37721;?#28000;?#37930;嬪埌Go涓?#37713;?#37825;板拰鍏?#28000;?#29831;?#29815;涓?#37928;勫?#36776;?#37724;岀壒鎬?/p>

鍏辨?/h3>
  • 鍏?#38335;?#28699;?#8212;—func
  • 鏂规硶鍚?#8212;—funcName
  • 鍏?#37721;?#8212;—— a,b int,b string
  • 杩斿?#28850;?#8212;— r,s int
  • 鍑?#37825;?#28003;?#8212;— {}

?#22738;鎬?/h3>

Go涓?#37713;?#37825;?#37928;勭壒鎬槸闈?#29999;R叿鐨?#38171;?#32513;欐垜浠?#29999;?#37833;?#28051;?#28051;鏍?#37928;勭?#26667;浣?#26976;?#37510;?/p>

涓虹壒瀹氱?#35826;?#23338;?#27691;箟鍑?#37825;?#38171;?#37719;?#28051;虹?#35826;?#23338;?#30789;薄瀹氫箟鏂规硶

?#28266;Go涓?#38315;?#26473;?#32513;?#37713;?#37825;版爣鏄?#37813;灞?#32491;诲瀷锛屾潵缁?#29831;?#32491;诲?#23338;?#27691;箟鏂规硶锛?#28051;?#38344;?#37928;?nbsp;p myType 鍗?#29723;?#32448;?#32513;myType澹版槑浜?#28051;涓?#37826;规硶锛?nbsp;p myType 涓嶆槸?#32320;椤?#37928;?#37510;?#28641;傛灉娌?#37832;?#38171;屽?#27409;函绮规槸涓涓?#37713;?#37825;?#38171;?#38315;?#26473;囧寘鍚嶇璁块棶銆俻ackageName.funcationName

濡?#38171;?/p>

//瀹氫箟鏂?#37928;?#32491;诲瀷double锛?#28051;?#29781;?#37929;?#37928;勬槸缁檉loat64绫诲瀷鎵?#37711;?#37826;规硶 type double float64  //鍒?#37826;a?#27128;鍚瓑浜b func (a double) IsEqual(b double) bool {     var r = a - b     if r == 0.0 {         return true     } else if r < 0.0 {         return r > -0.0001     }     return r < 0.0001 }  //鍒?#37826;a?#27128;鍚瓑浜b func IsEqual(a, b float64) bool {     var r = a - b     if r == 0.0 {         return true     } else if r < 0.0 {         return r > -0.0001     }     return r < 0.0001 }  func main() {     var a double = 1.999999     var b double = 1.9999998     fmt.Println(a.IsEqual(b))     fmt.Println(a.IsEqual(3))     fmt.Println( IsEqual( (float64)(a), (float64)(b) ) )  } 

涓?#26473;?#32448;轰緥涓?float64 ?#29056;鏈?#32491;诲瀷鎵?#37711;?#27996;?#37826;规硶IsEqual锛?#29831;?#37826;规硶涓?#29781;佹槸瑙?#37712;崇?#24807;害?#26870;棰?#37510;?鍏舵?#35268;?#26337;皟鐢?#37826;瑰紡涓?#38171;?nbsp;a.IsEqual(double) 锛?#28641;傛灉涓?#37813;?#37711;?#37826;规硶锛屾垜浠?#37721;兘浣?#37922;?#37713;?#37825;?code style="margin: 0px 2px; padding: 0px 5px; border: 1px solid #eaeaea; background-color: #f8f8f8; border-radius: 3px; white-space: nowrap; font-family: monospace, Monaco;">IsEqual(a, b float64)

鍏?#37721;備腑锛?#28641;傛灉杩?#32513;?#37928;勫?#20635;暟绫诲瀷涓鑷?#38171;屽垯鍙?#28000;?#37930;佺暐杩?#32513;?#28598;?#28051;?#37721;傛暟鐨?#32491;诲瀷锛屽彧淇濈暀鏈鍚?#28051;涓?#32491;诲?#23338;?#29256;槑銆?/h4>

濡?nbsp;func IsEqual(a, b float64) bool 杩?#28051;?#37826;规硶灏卞彧淇濈暀浜?#28051;涓?#32491;诲?#23338;?#29256;槑,姝?#37827;?#37711;?#37721;a?#25328;b鍧囨槸float64鏁版嵁绫诲瀷銆?杩欐牱涔熸槸鍙?#28000;?#37928;?#38171;?nbsp;func IsEqual(a, b float64, accuracy int) bool

鍙樺弬锛?#37711;?#37721;?#37824;?#37816;?#37721;樺弬,鍗?#37721;帴鍙?#28051;嶇瀹?#37825;?#38322;?#37928;勫悓涓绫诲瀷鐨勫?#20635;暟

濡?nbsp;func Sum(args ...int) 鍙傛暟args?#27128;鐨slice锛?#37711;?#37711;?#32497;?#32491;诲瀷涓篿nt 銆?#32513;?#29999;?#28003;?#37922;?#37928;fmt.Printf灏辨槸涓涓帴鍙?#28000;绘剰涓?#37825;板?#20635;暟鐨勫嚱鏁?nbsp;fmt.Printf(format string, args ...interface{})

鏀?#37816;?#28598;?#26473;斿?#28850;?/h4>

鍓?#38344;垜浠?#28729;氫箟鍑?#37825;?#37827;?#26473;斿?#28850;?#37832;?#28051;?#28051;猺,s 銆?#26473;欐槸闈?#29999;?#37832;?#37922;?#37928;?#38171;屾?#25117;湪鍐C#浠?#37934;?#37827;?#38171;?#29999;?#29999;?#28051;?#27996;嗕粠宸?#37832;?#37713;?#37825;?#28051;?#38014;峰緱鏇?#28598;?#37928;勪俊鎭?#38171;岄渶瑕?#28103;?#37824;?#37713;?#37825;扮鍚?#38171;?#28003;?#37922;out ,ref ?#29905;鏂瑰紡鍘昏?#23792;緱鏇?#28598;?#26473;斿洖缁撴灉銆?#38000;岀幇?#28266;浣?#37922;Go鏃?#37714;?#23536;?#32480;?#23839;锛?#37929;存帴?#28266;杩斿?#28850;?#37722;?#38344;?#23075;?#37716;?#26473;斿?#28850;?#20635;?#26495;嵆鍙?#37510;?/p>

濡??#28266;C#涓?#28051;涓?#28699;?#32471;?#28051;茶浆鎹?#28051;篿nt绫诲瀷鏃?#38315;昏緫浠?#37934;?/p>

int v=0;  if ( int.TryPase("123456",out v) ) {     //code } 

鑰屽湪Go涓?#38171;屽垯鍙?#28000;?#26473;欐牱瀹炵幇,閫昏?#25120;簿绠鑰屾?#24227;

if v,isOk :=int.TryPase("123456") ; isOk {     //code } 

鍚?#37827;跺湪Go涓?#23536;?#28598;氬嚱鏁?#37711;呭垎鍒?#37922;?#27996;嗗杩斿?#28850;?/p>

  • func (file *File) Write(b []byte) (n int, err error)
  • func Sincos(x float64) (sin, cos float64)

閭箞濡傛灉鎴戝彧?#28214;瑕佹?#24874;竴涓?#26473;斿?#28850;?#38171;?#38000;?#28051;?#37711;?#36423;?#37711;?#28000;?#26473;斿?#28850;?#37928;勮瘽锛屾垜璇?#28641;備綍鍔炲憿锛?杩欐椂鍙?#28000;?#32480;?#23839;鐨?#28003;?#37922;?#32471;?#37721;?#28051;嬪垝绾?#8221;_“ 鏉?#36423;界暐涓?#37711;?#36423;?#37928;?#26473;斿?#28850;?#37510;?#28641;?#38171;?/p>

_, cos = math.Sincos(3.1415) //鍙渶瑕乧os璁?#32480;?#37928;勫?

鍛?#37722;?#26473;斿?#28850;?/h4>

鍓?#38344;垜浠?#29831;?#27996;?#37713;?#37825;板彲浠?#37832;夊涓?#26473;斿?#28850;?#38171;?#26473;欓?#23678;垜杩樿?#20349;鐨勬槸锛屽湪鍑?#37825;?#28729;氫箟鏃?#37721;?#28000;?#32513;欐墍鏈?#37928;?#26473;斿?#28850;煎垎鍒?#37723;?#37722;?#38171;?#26473;欐牱灏辫兘?#28266;鍑?#37825;?#28051;?#28000;绘剰浣嶇疆缁?#28051;嶅悓杩斿?#28850;?#28598;嶅埗锛?#38000;?#28051;嶉渶瑕佸湪return璇?#37721;?#28051;?#37813;?#37816;?#28729;?#26473;斿?#28850;?#37510;?#37722;?#37827;朵?#29115;兘澧炲?#21700;彲璇?#37804;?#38171;屼?#29112;彁楂榞odoc鎵鐢熸垚?#26499;妗?#37928;勫彲璇?#37804;?/p>

濡傛灉涓?#37824;?#37816;?#37723;?#37722;?#26473;斿?#28850;?#38171;屾?#25117;彲?#20824;?#32048;?#27128;杩欐?#23792;仛鐨?/p>

func ReadFull(r Reader, buf []byte) (int, error) {     var n int     var err error      for len(buf) > 0  {         var nr int         nr, err = r.Read(buf)          n += nr         if err !=nil {             return n,err         }         buf = buf[nr:]     }     return n,err } 

浣?#37824;?#37816;?#32513;?#26473;斿?#28850;?#37723;?#37722;嶅悗锛?#28729;?#38340;?#28051;?#28751;辨槸鐪佺暐浜嗗彉閲?#37928;勫?#29256;槑锛宺eturn鏃?#37827;犻渶鍐欐垚return n,err 鑰屾槸灏?#37929;存帴灏嗗?#26473;斿洖

func ReadFull(r Reader, buf []byte) (n int, err error) {     for len(buf) > 0 && err == nil {         var nr int         nr, err = r.Read(buf)         n += nr         buf = buf[nr:]     }     return } 

鍑?#37825;?#28052;熸槸“鍊?#8221;

?#25328;Go涓?#37711;?#28000;?#28051;滆涓鏍?#38171;?#37713;?#37825;?#28052;熸槸鍊?#38171;?#26473;欐牱灏卞彲浠?#28601;版槑涓涓?#37713;?#37825;?#32491;诲瀷鐨勫彉閲?#38171;?#28751;?#37713;?#37825;?#28003;?#28051;哄?#20635;暟浼?#38315;?#37510;?/p>

澹版槑鍑?#37825;?#28051;哄?#37928;勫彉閲?鍖垮?#23941;嚱鏁?鍙?#29863;嬪?#28051;?#37721;?#38322;?#38171;屼篃鍙?#37929;存帴鎵?#29723;?

//璧嬪?fc := func(msg string) {     fmt.Println("you say :", msg) } fmt.Printf("%T \n", fc) fc("hello,my love") //鐩存帴鎵?#29723;?func(msg string) {     fmt.Println("say :", msg) }("I love to code") 

杈?#37713;?#32513;撴灉濡備笅锛?#26473;欓?#23683;鏄巉c 鐨?#32491;诲瀷涓?#38171;歠unc(string)

func(string)  you say : hello,my love say : I love to code 

灏?#37713;?#37825;?#28003;?#28051;?#37711;?#37721;?#38171;?#37733;?#29835;冨嚱鏁?#38171;?#38171;岃兘甯?#37833;?#28186;垮埄銆?#28641;?#37827;?#36423;?#28598;勭悊锛?#28051;?#27996;?#32513;?#28051;澶勭悊锛?#28751;嗕俊鎭?#37735;?#38315;?#26473;?#37816;?#28729;氬嚱鏁板幓璁板綍鏃?#36423;?#38171;?#28051;旀槸鍚?#29825;板綍鏃?#36423;?#26473;?#37832;夊紑鍏?/p>

func Log(title string, getMsg func() string) {     //濡傛灉寮鍚?#37827;?#36423;?#29825;板綍,鍒?#29825;板綍鏃?#36423;?    if true {         fmt.Println(title, ":", getMsg())     } } //---------璋?#37922;?------------- count := 0 msg := func() string {     count++     return "?#20557;娌?#37832;?#37719;?#28003;?#37819;?#38321;掓垜,宸?#29785;?#37912;?#23049;曞緥" } Log("error", msg) Log("warring", msg) Log("info", msg) fmt.Println(count) 

杩欓?#23683;鍑?#32513;撴灉濡備笅锛宑ount 涔熷彂鐢?#27996;嗗?#27194;寲

error : ?#20557;娌?#37832;?#37719;?#28003;?#37819;?#38321;掓垜,宸?#29785;?#37912;?#23049;曞緥 warring : ?#20557;娌?#37832;?#37719;?#28003;?#37819;?#38321;掓垜,宸?#29785;?#37912;?#23049;曞緥 info : ?#20557;娌?#37832;?#37719;?#28003;?#37819;?#38321;掓垜,宸?#29785;?#37912;?#23049;曞緥 3 

鍑?#37825;?#28052;熸槸“绫诲瀷”

浣?#37832;?#23052;?#37832;夋敞鎰忓埌涓?#38344;?#32448;轰緥涓?#37928;?nbsp;fc := func(msg string)... 锛?#37827;?#37906;?#37718;垮?#23941;嚱鏁板彲浠?#29863;嬪?#32513;?#28051;涓?#37721;?#38322;?#38171;?#37722;?#37827;舵垜浠?#32513;?#29999;?#26473;欐牱缁檌nt璧嬪?nbsp;value := 2 ,?#27128;鍚垜浠?#37721;?#28000;?#28601;版槑func(string) 绫诲瀷 鍛?#38171;?#35120;撶?#33333;槸鍙?#28000;?#37928;?#37510;?/p>

//涓涓?#29825;板綍鏃?#36423;?#37928;?#32491;诲瀷锛歠unc(string) type saveLog func(msg string)  //灏?#28699;?#32471;?#28051;茶浆鎹?#28051;篿nt64,濡傛灉?#27974;鎹?#28598;?#29840;?#29835;?#37922;saveLog func stringToInt(s string, log saveLog) int64 {      if value, err := strconv.ParseInt(s, 0, 0); err != nil {         log(err.Error())         return 0     } else {         return value     } }  //璁板綍鏃?#36423;楁秷鎭?#37928;勫叿浣?#28729;炵幇 func myLog(msg string) {     fmt.Println("Find Error:", msg) }  func main() {     stringToInt("123", myLog) //?#27974;鎹?#37827;?#28751;嗚皟鐢mylog璁板綍鏃?#36423;?    stringToInt("s", myLog) } 

杩欓?#23678;垜浠?#28729;氫箟浜?#28051;涓?#32491;诲瀷锛?#28051;?#38338;?#37922;?#28003;?#29825;板綍鏃?#36423;?#37928;勬?#22247;?#21976;帴鍙?#37510;傚湪stringToInt鍑?#37825;?#28051;?#28641;傛灉?#27974;鎹?#28598;?#29840;?#37714;?#29835;?#37922;垜鑷繁瀹氫箟鐨勬帴鍙?#37713;?#37825;?#26473;?#29723;?#37827;?#36423;?#28598;勭悊锛?#38007;?#27996;?#37832;缁?#37813;?#29723;?#37928;勫摢涓?#37713;?#37825;?#38171;屽?#27408;棤?#28214;鍏?#36423;?#37510;?/p>

defer 寤?#26473;?#37713;?#37825;?/h4>

defer 鍙堟槸涓涓?#37714;?#37826;?#38171;?#28729;?#37928;?#28003;?#37922;槸锛氬欢杩熸墽琛?#38171;屽湪澹版槑鏃?#28051;嶄?#27697;?#23338;嵆鎵?#29723;?#38171;?#38000;屾槸?#28266;鍑?#37825;皉eturn鍚?#37827;舵?#22796;収鍚?#26473;?#37711;?#37713;?#37928;勫師鍒?#28186;?#23046;?#37813;?#29723;屾瘡涓涓猟efer銆?#26473;欐牱甯?#37833;?#37928;勫澶勬槸锛岃兘纭?#28103;濇垜浠?#28729;氫箟鐨勫嚱鏁拌兘鐧惧垎?#31651;鐧捐兘澶熻鎵?#29723;屽埌锛?#26473;欐牱灏辫兘鍋氬緢澶氭垜浠?#37807;冲仛鐨?#27996;?#38171;?#28641;?#38322;?#37824;捐?#21228;簮锛屾?#21615;悊鏁版嵁锛?#29825;板綍鏃?#36423;楃瓑

杩欓?#23678;垜浠?#38322;?#37904;?#37833;?#29831;?#37828;?#28051;defer鐨?#37813;?#29723;?#26916;哄簭

func deferFunc() int {     index := 0      fc := func() {          fmt.Println(index, "鍖垮?#23941;嚱鏁?")         index++          defer func() {             fmt.Println(index, "鍖垮?#23941;嚱鏁?-1")             index++         }()     }      defer func() {         fmt.Println(index, "鍖垮?#23941;嚱鏁?")         index++     }()      defer fc()      return func() int {         fmt.Println(index, "鍖垮?#23941;嚱鏁?")         index++         return index     }() }  func main() {     deferFunc() } 

杩欓?#23683;鍑?#32513;撴灉濡備笅锛?/p>

0 鍖垮?#23941;嚱鏁? 1 鍖垮?#23941;嚱鏁? 2 鍖垮?#23941;嚱鏁?-1 3 鍖垮?#23941;嚱鏁? 

鏈?#28641;備笅缁?#29825;?#38171;?/p>

  • defer ?#27128;?#28266;鎵?#29723;?#28729;宺eturn 鍚?#37813;?#29723;?/li>
  • defer 鍚?#26473;?#37711;?#37813;?#29723;?/li>

鍙?#28598;?#38171;屾垜浠?#29999;?#28003;?#37922;defer鍘?#37711;?#38338;璉O,?#28266;姝?#29999;?#37813;?#23534;?#26499;浠?#37722;?#38171;?#28751;?#32468;嬪?#35826;?#29256;槑涓涓猟efer锛?#26473;欐牱灏?#28051;嶄?#27692;?#27199;鍏?#38338;枃浠?#38171;屼?#29115;兘淇濊瘉?#28266;鍑虹幇寮?#29999;哥瓑涓嶅彲棰?#37826;?#37928;勬鍐?#28051;?#28052;熻兘鍏?#38338;枃浠?#37510;?#38000;?#28051;嶅鍏?#28000;?#29831;?#29815;锛?code style="margin: 0px 2px; padding: 0px 5px; border: 1px solid #eaeaea; background-color: #f8f8f8; border-radius: 3px; white-space: nowrap; font-family: monospace, Monaco;">try-catch 鎴?#38000;?nbsp;using() 鏂瑰紡杩?#29723;屽?#21229;悊銆?/p>

file , err :=os.Open(file) if err != nil {     return err } defer file.Close()  //dosomething with file 

鍚?#32513;?#38171;屾垜灏?#29825;?#29825;?#38171;?浣?#37922;?#37737;?#37510;佷紶鍊煎?#23676;紶鎸?#38333;?浠?#37721;?淇濈暀鍑?#37825;init(),main()

鏈?#32471;?#29825;?#28051;?#37813;鍐?#28000;?#37934;?#28699;樺偍浣嶇疆锛?/p>



abin 2017-08-02 16:39 ?#24386;琛?#29831;勮
]]>MySQL瑙?#37721;戝櫒Trigger瀹炰?#23340;瘒 http://www.5432034.com/stevenjohn/archive/2016/08/18/431625.htmlabinabinThu, 18 Aug 2016 09:25:00 GMThttp://www.5432034.com/stevenjohn/archive/2016/08/18/431625.htmlhttp://www.5432034.com/stevenjohn/comments/431625.htmlhttp://www.5432034.com/stevenjohn/archive/2016/08/18/431625.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/431625.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/431625.html
MySQL瑙?#37721;戝櫒Trigger瀹炰?#23340;瘒
?#24386;琛?#27996;?68 澶?#37715;?/span> ⁄ IT?#22951;鏈?/a> ⁄ 鏆?#37827;?#29831;勮

浠?#37715;?#37711;?#23049;?#37928;?#37825;版嵁瀛樺偍杩?#32459;?#28051;嶅?#22118;鍏?#28051;?#28610;?#28641;?#38171;?#37832;杩?#38316;囧埌?#27861;鏁版嵁搴?/a>锛?#37722;?#37827;?#28725;瑰涓?#29723;?#26473;?#29723;孋URD锛圕reate澧?#37510;乁pdate鏀?#37510;丷ead璇?#37510;丏elete鍒?#38171;?#38171;屾庝箞鎵?#38003;借绻佺鐨?#37825;版嵁CURD鍚屾鍙樺緱鏇?#28729;规鍛?#38171;?#37929;镐俊寰?#28598;?#27996;轰細棣?#37711;?#37807;冲埌浜?a href="http://lib.csdn.net/base/14" title="MySQL?#29025;璇?#25652;? target="_blank" style="color:#df3434; font-weight:bold;">MySQL瀛樺偍杩?#32459;?#37510;佽鍙戝櫒锛?#26473;?#32457;嶆兂?#30838;纭?#28729;?#28051;嶉敊銆備簬?#27128;?#37832;?#37711;磋鍦?#27996;?#38007;?#37712;?#27996;CUD锛?#28583;?#37510;?#37824;?#37510;?#37714;?#38171;?#29785;?#37721;戝櫒鐨勫疄渚?#38171;?#37922;?#29785;?#37721;戝櫒瀹炵幇澶氳鏁版嵁鍚屾鏇?#37826;?#37510;?/p>

MySQL瑙?#37721;戝櫒Trigger瀹炰?#23340;瘒

瀹氫箟锛??#32141;涓篗ySQL瑙?#37721;戝櫒锛?/strong>

?#28266;MySQL Server閲?#38344;?#28052;?#28751;辨槸瀵?#37836;愪竴涓?#29723;?#37928;?#28051;瀹?#37928;勬搷浣?#38171;?#29785;彂鏌愮鏉?#28000;?#38171;圛nsert,Update,Delete ?#29905;锛?#38171;屼粠鑰?#38007;?#37716;?#37813;?#29723;?#37928;?#28051;娈?#32459;嬪簭銆備粠杩?#32457;嶆?#24526;箟涓?#29825;?#29785;?#37721;戝櫒?#27128;涓涓?#37911;规鐨勫?#27194;偍杩?#32459;?#37510;備笅闈?#38315;?#26473;MySQL瑙?#37721;戝櫒瀹炰緥锛屾潵浜?#29785;?#28051;涓?#29785;?#37721;戝櫒鐨勫伐浣?#26473;?#32459;嬪惂锛?/p>

涓銆?#37714;?#23524;篗ySQL瀹炰緥鏁版嵁琛?#38171;?/strong>

?#28266;mysql鐨勯?#27199;鐨勬祴璇test鏁版嵁搴?#28051;?#38171;屽垱寤?#28051;?#28051;?#29723;t_a涓巘_b锛?/p>



    /*Table structure for table `t_a` */
    DROP TABLE IF EXISTS `t_a`;
    CREATE TABLE `t_a` (
      `id` smallint(1) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(20) DEFAULT NULL,
      `groupid` mediumint(8) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
     
    /*Data for the table `t_a` */
    LOCK TABLES `t_a` WRITE;
    UNLOCK TABLES;
     
    /*Table structure for table `t_b` */
    DROP TABLE IF EXISTS `t_b`;
    CREATE TABLE `t_b` (
      `id` smallint(1) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(20) DEFAULT NULL,
      `groupid` mediumint(8) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=57 DEFAULT CHARSET=latin1;
     
    /*Data for the table `t_b` */
    LOCK TABLES `t_b` WRITE;
    UNLOCK TABLES;

?#28266;t_a琛?#28051;婂垎鍒?#23524;?#28051;涓狢UD锛?#28583;?#37510;?#37824;?#37510;?#37714;?#38171;?涓?#29785;?#37721;戝櫒锛?#28751;t_a鐨勮鏁版嵁涓巘_b鍚屾瀹炵幇CUD锛屾敞鎰忓垱寤?#29785;?#37721;戝櫒?#30241;涓?#29723;?#37722;?#32491;?#27996;?#28000;?#37832;?#28051;?#28000;?#37832;?#28051;涓?#28725;?#25652;?#29785;?#37721;戝櫒锛?#28051;?#28000;?#31646;鍙兘瀵?#28051;涓?#29785;?#37721;戝櫒锛?#28051;?#29785;?#38322;婂锛岀湅MYSQL鐨勮鏄?#29999;?#37716;枃妗?#37722;?#37510;?/p>

浜?#37510;?#37714;?#23524;篗ySQL瀹炰緥瑙?#37721;戝櫒锛?/strong>

?#28266;瀹炰緥鏁版嵁琛t_a涓?#28186;?#23046;?#37816;夌収涓?#38344;?#23005;?#26976;?#37714;?#23524;簍r_a_insert銆乼r_a_update銆乼r_a_delete涓?#28051;?#29785;?#37721;戝櫒

1銆?#37714;?#23524;篒NSERT瑙?#37721;戝櫒trigger_a_insert锛?/strong>



    DELIMITER $$
     
    USE `test`$$
     
    --鍒?#37826;?#37825;版嵁搴?#28051;槸鍚?#28699;樺湪tr_a_insert瑙?#37721;戝櫒
    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_insert`$$
    --涓嶅?#27194;湪tr_a_insert瑙?#37721;戝櫒锛屽紑濮嬪垱寤?#29785;?#37721;戝櫒
    --Trigger瑙彂鏉?#28000;?#28051;篿nsert鎴?#37716;?#37722;?#26473;?#29723;?#29785;彂
    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_insert` AFTER INSERT ON `t_a`
        FOR EACH ROW BEGIN
            --Trigger瑙?#37721;戝悗锛?#37722;?#37827;?#28725;箃_b鏂?#28583;?#37722;屾涓鏉?#37825;版嵁
            INSERT INTO `t_b` SET username = NEW.username, groupid=NEW.groupid;
        END;
    $$
     
    DELIMITER;
2銆?#37714;?#23524;篣PDATE瑙?#37721;戝櫒trigger_a_update锛?/strong>


    DELIMITER $$
     
    USE `test`$$
    --鍒?#37826;?#37825;版嵁搴?#28051;槸鍚?#28699;樺湪tr_a_update瑙?#37721;戝櫒
    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_update`$$
    --涓嶅?#27194;湪tr_a_update瑙?#37721;戝櫒锛屽紑濮嬪垱寤?#29785;?#37721;戝櫒
    --Trigger瑙彂鏉?#28000;?#28051;簎pdate鎴?#37716;?#37722;?#26473;?#29723;?#29785;彂
    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_update` AFTER UPDATE ON `t_a`
        FOR EACH ROW BEGIN
        --Trigger瑙?#37721;戝悗锛?#35120;搕_a琛groupid,username鏁版嵁鏈夋洿鏀?#37827;?#38171;屽?#31619;_b琛?#37722;屾涓鏉?#37831;?#37826;?#37722;?#37928;?#37825;版嵁
          IF new.groupid != old.groupid OR old.username != new.username THEN
            UPDATE `t_b` SET groupid=NEW.groupid,username=NEW.username WHEREusername=OLD.username AND groupid=OLD.groupid;
          END IF;
              
        END;
    $$
     
    DELIMITER ;
3銆?#37714;?#23524;篋ELETE瑙?#37721;戝櫒trigger_a_delete锛?/strong>


    DELIMITER $$
     
    USE `test`$$
    --鍒?#37826;?#37825;版嵁搴?#28051;槸鍚?#28699;樺湪tr_a_delete瑙?#37721;戝櫒
    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_delete`$$
    --涓嶅?#27194;湪tr_a_delete瑙?#37721;戝櫒锛屽紑濮嬪垱寤?#29785;?#37721;戝櫒
    --Trigger瑙彂鏉?#28000;?#28051;篸elete鎴?#37716;?#37722;?#26473;?#29723;?#29785;彂
    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_delete` AFTER DELETE ON `t_a`
        FOR EACH ROW BEGIN
            --t_a琛?#37825;版嵁鍒犻櫎鍚?#38171;宼_b琛?#37711;?#38001;?#37833;?#28000;?#37929;?#37722;?#37928;?#37825;版嵁涔?#37722;屾鍒犻櫎
            DELETE FROM `t_b` WHERE username=Old.username AND groupid=OLD.groupid;
        END;
    $$
     
    DELIMITER ;

涓?#37510;佹祴璇昅ySQL瀹炰緥瑙?#37721;戝櫒锛?/strong>

?#22414;鍒?#23092;?#29831;?#28729;炵幇t_a涓巘_b瀹炵幇鏁版嵁鍚屾CUD(澧?#37510;?#37824;?#37510;?#37714;?3涓猅riggers

1銆佹祴璇昅ySQL鐨勫疄渚媡r_a_insert瑙?#37721;戝櫒锛?/strong>

?#28266;t_a琛?#28051;?#37826;?#28583;?#28051;鏉?#37825;版嵁锛岀劧鍚庡垎鍒?#37836;?#29831;t_a/t_b琛?#37928;?#37825;版嵁?#27128;鍚?#37825;版嵁鍚屾锛屾祴璇曡鍙戝櫒鎴?#37716;熸?#22247;織锛宼_a琛?#37827;?#29825;哄湪?#32141;绉嶆鍐?#28051;?#38171;屾柊澧?#27996;?#28051;鏉?#37812;?#28598;?#37833;?#29825;板綍闆?#37827;?#38171;屾病鏈塼_b琛仛浠讳綍鏁版嵁insert鎿?#28003;?#38171;?#28729;冨悓鏃舵柊澧?#27996;?#28051;鏍?#37928;勫鏉?#29825;板綍闆?#37510;?/p>

涓?#38344;?#37833;?#26473;?#29723;孧ySQL瑙?#37721;戝櫒瀹炰緥娴?#29831;?#38171;?/p>



    --t_a琛?#37826;?#28583;?#28051;鏉?#29825;板綍闆?br />        INSERT INTO `t_a` (username,groupid) VALUES ('sky54.net',123)
       
        --鏌?#29831;t_a琛?br />        SELECT id,username,groupid FROM `t_a`
       
        --鏌?#29831;t_b琛?br />        SELECT id,username,groupid FROM `t_b`

2銆佹祴璇昅ySQL鐨勫疄渚媡r_a_update銆乼r_a_delete瑙?#37721;戝櫒锛?/strong>

杩?#28051;?#28051;狹ySQL瑙?#37721;戝櫒娴?#29831;曞?#29114;悊銆佹楠?#28051;巘r_a_insert瑙?#37721;戝櫒涓鏍?#37928;?#38171;?#37711;?#28103;?#37824;?鍒犻櫎涓鏉?#37825;版嵁锛岀劧鍚庡垎鍒?#37836;湅t_a銆乼_b琛?#37928;?#37825;版嵁鍙樺寲鎯?#37712;?#38171;屾?#29256;嵁鍙樺寲鍚屾璇?#37828;嶵rigger瀹炰緥鎴?#37716;?#38171;?#37722;?#37714;欓渶瑕?#38315;?#23005;?#37818;掓煡閿?#29831;?#37720;?#37733;?#37510;?/p>

涓栫晫涓?#28000;讳綍涓绉?#27996;嬬閮?#37711;?#37711;朵紭鐐瑰?#23680;?#34425;偣锛屼紭鐐?#28051;庣?#34425;?#35268;槸鑷韩涓涓?#37929;?#28725;圭珛鐨?#38344;?#37510;?#35120;撶劧杩欓噷涓嶆槸?#24049;璋?#8220;涓栫晫闈為?#25117;?#23815;櫧”?#32033;鐨?#8220;浜?#37711;?#29825;?#8221;锛?#8220;瀛樺湪鍗?#37722;堢悊”鍢?#37510;?#35120;撶劧 MySQL瑙?#37721;戝櫒鐨勪紭鐐?#28051;?#29831;?#27996;?#38171;?#29831;?#28051;涓?#28051;嶈涔嬪锛孧ySQL Trigger娌?#37832;?#23536;堝鐨勮皟璇?#37510;佺?#24714;鐜?#28583;?#38171;岄毦浜庡湪鍚勭?#23943;郴缁?#37916;?#28583;?#28051;?#23092;?#29831;?#38171;屾祴璇曟瘮MySQL瀛樺偍杩?#32459;?#29781;?#38341;?#38171;屾墍浠?#23524;?#29825;湪鐢熸垚鐜?#28583;?#28051;?#38171;?#28751;?#38322;?#37922;?#28699;樺偍杩?#32459;?#37833;?#28000;?#37831;?MySQL瑙?#37721;戝櫒銆?/p>

鏈?#32481;?#32513;撴潫鍓嶅?#23941;己璋?#28051;涓?#38171;?#37824;?#37816;佽鍙戝櫒鐨凪ySQL鐗?#37832;渶瑕?.0浠?#28051;?#38171;?.0浠?#37715;嶇鏈?#37928;凪ySQL鍗囩骇?#22476;5.0浠?#37722;庣鏈?#37826;瑰彲浣?#37922;?#29785;?#37721;戝櫒鍝?#38171;?/p>








http://blog.csdn.net/hireboy/article/details/18079183





abin 2016-08-18 17:25 ?#24386;琛?#29831;勮
]]>
鑱?#38001;?#26946;樺苟?#24386;?#37108;缁熶箣闄?#23092;佺?#35268;妧-1http://www.5432034.com/stevenjohn/archive/2016/06/14/430882.htmlabinabinTue, 14 Jun 2016 05:38:00 GMThttp://www.5432034.com/stevenjohn/archive/2016/06/14/430882.htmlhttp://www.5432034.com/stevenjohn/comments/430882.htmlhttp://www.5432034.com/stevenjohn/archive/2016/06/14/430882.html#Feedback1http://www.5432034.com/stevenjohn/comments/commentRss/430882.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/430882.html闃呰鍏枃

abin 2016-06-14 13:38 ?#24386;琛?#29831;勮
]]>
shadowsocks 瀹夎http://www.5432034.com/stevenjohn/archive/2016/05/13/430493.htmlabinabinFri, 13 May 2016 14:56:00 GMThttp://www.5432034.com/stevenjohn/archive/2016/05/13/430493.htmlhttp://www.5432034.com/stevenjohn/comments/430493.htmlhttp://www.5432034.com/stevenjohn/archive/2016/05/13/430493.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/430493.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/430493.htmlInstall the Command Line Client

If you prefer command line client, then you can install it on your Linux with the following command.

Debian

sudo apt-get install python-pip sudo pip install shadowsocks

Ubuntu

Yes, you can use the above commands to install shadowsocks client on ubuntu. But it will install it under ~/.local/bin/ directory and it causes loads of trouble. So I suggest using su to become root first and then issue the following two commands.

apt-get install python-pip pip install shadowsocks

Fedora/Centos

sudo yum install python-setuptools   or   sudo dnf install python-setuptools sudo easy_install pip sudo pip install shadowsocks

OpenSUSE

sudo zypper install python-pip sudo pip install shadowsocks

Archlinux

sudo pacman -S python-pip sudo pip install shadowsocks

As you can see the command of installing shadowsocks client is the same to the command of installing shadowsocks server, because the above command will install both the client and the server. You can verify this by looking at the installation script output

Downloading/unpacking shadowsocks Downloading shadowsocks-2.8.2.tar.gz Running setup.py (path:/tmp/pip-build-PQIgUg/shadowsocks/setup.py) egg_info for package shadowsocks  Installing collected packages: shadowsocks Running setup.py install for shadowsocks  Installing sslocal script to /usr/local/bin Installing ssserver script to /usr/local/bin Successfully installed shadowsocks Cleaning up...

sslocal is the client software and ssserver is the server software. On some Linux distros such as ubuntu, the shadowsocks client sslocal is installed under /usr/local/bin. On Others such as Archsslocal is installed under /usr/bin/. Your can use whereis command to find the exact location.

user@debian:~$ whereis sslocal sslocal: /usr/local/bin/sslocal

Create a Configuration File

we will create a configuration file under /etc/

sudo vi /etc/shadowsocks.json

Put the following text in the file. Replace server-ip with your actual IP and set a password.

{
"server":"server-ip",
"server_port":8000,
"local_address": "127.0.0.1",
"local_port":1080,
"password":"your-password",
"timeout":600,
"method":"aes-256-cfb"
}

Save and close the file. Next start the client using command line

sslocal -c /etc/shadowsocks.json

To run in the background

sudo sslocal -c /etc/shadowsocks.json -d start

Auto Start the Client on System Boot

Edit /etc/rc.local file

sudo vi /etc/rc.local

Put the following line above the exit 0 line:

sudo sslocal -c /etc/shadowsocks.json -d start

Save and close the file. Next time you start your computer, shadowsocks client will automatically start and connect to your shadowsocks server.

Check if It Works

After you rebooted your computer, enter the following command in terminal:

sudo systemctl status rc-local.service

If your sslocal command works then you will get this ouput:


● rc-local.service - /etc/rc.local 

Compatibility Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2015-11-27 03:19:25 CST; 2min 39s ago
Process: 881 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/rc-local.service
├─ 887 watch -n 60 su matrix -c ibam
└─1112 /usr/bin/python /usr/local/bin/sslocal -c /etc/shadowsocks....

As you can see from the last line, the sslocal command created a process whose pid is 1112 on my machine. It means shadowsocks client is running smoothly. And of course you can tell your browser to connect through your shadowsocks client to see if everything goes well.

If for some reason your /etc/rc.local script won’t run, then check the following post to find the solution.

How to enable /etc/rc.local with SystemdInstall the Command Line Client

If you prefer command line client, then you can install it on your Linux with the following command.

Debian

sudo apt-get install python-pip
sudo pip install shadowsocks

Ubuntu

Yes, you can use the above commands to install shadowsocks client on ubuntu. But it will install it under ~/.local/bin/ directory and it causes loads of trouble. So I suggest using su to become root first and then issue the following two commands.

apt-get install python-pip
pip install shadowsocks

Fedora/Centos

sudo yum install python-setuptools   or   sudo dnf install python-setuptools
sudo easy_install pip
sudo pip install shadowsocks

OpenSUSE

sudo zypper install python-pip
sudo pip install shadowsocks

Archlinux

sudo pacman -S python-pip
sudo pip install shadowsocks

As you can see the command of installing shadowsocks client is the same to the command of installing shadowsocks server, because the above command will install both the client and the server. You can verify this by looking at the installation script output

Downloading/unpacking shadowsocks
Downloading shadowsocks-2.8.2.tar.gz
Running setup.py (path:/tmp/pip-build-PQIgUg/shadowsocks/setup.py) egg_info for package shadowsocks

Installing collected packages: shadowsocks
Running setup.py install for shadowsocks

Installing sslocal script to /usr/local/bin
Installing ssserver script to /usr/local/bin
Successfully installed shadowsocks
Cleaning up...

sslocal is the client software and ssserver is the server software. On some Linux distros such as ubuntu, the shadowsocks client sslocal is installed under /usr/local/bin. On Others such as Archsslocal is installed under /usr/bin/. Your can use whereis command to find the exact location.

user@debian:~$ whereis sslocal
sslocal: /usr/local/bin/sslocal

Create a Configuration File

we will create a configuration file under /etc/

sudo vi /etc/shadowsocks.json

Put the following text in the file. Replace server-ip with your actual IP and set a password.

{
"server":"server-ip",
"server_port":8000,
"local_address": "127.0.0.1",
"local_port":1080,
"password":"your-password",
"timeout":600,
"method":"aes-256-cfb"
}

Save and close the file. Next start the client using command line

sslocal -c /etc/shadowsocks.json

To run in the background

sudo sslocal -c /etc/shadowsocks.json -d start

Auto Start the Client on System Boot

Edit /etc/rc.local file

sudo vi /etc/rc.local

Put the following line above the exit 0 line:

sudo sslocal -c /etc/shadowsocks.json -d start

Save and close the file. Next time you start your computer, shadowsocks client will automatically start and connect to your shadowsocks server.

Check if It Works

After you rebooted your computer, enter the following command in terminal:

sudo systemctl status rc-local.service

If your sslocal command works then you will get this ouput:

● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2015-11-27 03:19:25 CST; 2min 39s ago
Process: 881 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/rc-local.service
├─ 887 watch -n 60 su matrix -c ibam
└─1112 /usr/bin/python /usr/local/bin/sslocal -c /etc/shadowsocks....

As you can see from the last line, the sslocal command created a process whose pid is 1112 on my machine. It means shadowsocks client is running smoothly. And of course you can tell your browser to connect through your shadowsocks client to see if everything goes well.

If for some reason your /etc/rc.local script won’t run, then check the following post to find the solution.

How to enable /etc/rc.local with Systemd






abin 2016-05-13 22:56 ?#24386;琛?#29831;勮
]]>
httpClient Https ?#23839;鍚?#28051;嶉?#23683;瘉(httpClient杩炴帴姹? http://www.5432034.com/stevenjohn/archive/2016/04/27/430267.htmlabinabinWed, 27 Apr 2016 11:04:00 GMThttp://www.5432034.com/stevenjohn/archive/2016/04/27/430267.htmlhttp://www.5432034.com/stevenjohn/comments/430267.htmlhttp://www.5432034.com/stevenjohn/archive/2016/04/27/430267.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/430267.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/430267.html搴?#29831;?#28751;戣锛?#37929;存帴涓?#28000;?#37934;?#38171;?#28000;?#37715;嶉兘?#27128;璋?#37922;?#37714;?#27996;哄啓濂?#37928;?#38171;岀幇?#28266;鏈?#37827;堕棿鑷?#23480;卞涓?#38171;?#37711;?#28003;?#37716;熻兘濡備笅锛?br />1銆乭ttpClient+http+绾?#32459;?#23033;?#38171;?br />2銆乭ttpClient+https(?#23839;鍚?#28051;嶉獙璇佽瘉涔?+绾?#32459;?#23033;?#38171;?br />
https?#28266;%TOMCAT_HOME%/conf/server.xml閲?#38344;?#37928;勯?#23943;疆?#26499;浠?br />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" 
     maxThreads="150" scheme="https" secure="true" 
     clientAuth="false" keystoreFile="D:/tomcat.keystore" 
     keystorePass="heikaim" sslProtocol="TLS"  executor="tomcatThreadPool"/> 
鍏?#28051;?nbsp;clientAuth="false"琛?#32448;?#28051;嶅紑鍚瘉涔?#26976;岃瘉锛屽彧?#27128;?#23839;瀛?#37928;勮蛋https



package com.abin.lee.util;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.*;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

/**
* Created with IntelliJ IDEA.
* User: abin
* Date: 16-4-18
* Time: 涓?#37719;?0:24
* To change this template use File | Settings | File Templates.
*/
public class HttpClientUtil {
private static CloseableHttpClient httpsClient = null;
private static CloseableHttpClient httpClient = null;

static {
httpClient = getHttpClient();
httpsClient = getHttpsClient();
}

public static CloseableHttpClient getHttpClient() {
try {
httpClient = HttpClients.custom()
.setConnectionManager(PoolManager.getHttpPoolInstance())
.setConnectionManagerShared(true)
.setDefaultRequestConfig(requestConfig())
.setRetryHandler(retryHandler())
.build();
} catch (Exception e) {
e.printStackTrace();
}
return httpClient;
}


public static CloseableHttpClient getHttpsClient() {
try {
//Secure Protocol implementation.
SSLContext ctx = SSLContext.getInstance("SSL");
//Implementation of a trust manager for X509 certificates
TrustManager x509TrustManager = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{x509TrustManager}, null);
//棣?#37711;?#29825;剧疆鍏?#28766;鐨勬?#22247;?#21755;ookie绛栫暐
// RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
ConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(ctx, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", connectionSocketFactory).build();
// 璁剧疆杩炴帴姹?br /> httpsClient = HttpClients.custom()
.setConnectionManager(PoolsManager.getHttpsPoolInstance(socketFactoryRegistry))
.setConnectionManagerShared(true)
.setDefaultRequestConfig(requestConfig())
.setRetryHandler(retryHandler())
.build();
} catch (Exception e) {
e.printStackTrace();
}
return httpsClient;
}

// 閰嶇疆璇?#23033;?#37928;勮秴鏃?#29825;剧疆
//棣?#37711;?#29825;剧疆鍏?#28766;鐨勬?#22247;?#21755;ookie绛栫暐
public static RequestConfig requestConfig(){
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD_STRICT)
.setConnectionRequestTimeout(20000)
.setConnectTimeout(20000)
.setSocketTimeout(20000)
.build();
return requestConfig;
}

public static HttpRequestRetryHandler retryHandler(){
//璇?#23033;?#38322;?#29831;曞?#21229;悊
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception,int executionCount, HttpContext context) {
if (executionCount >= 5) {// 濡傛灉宸?#32513;?#38322;?#29831;?#27996;?娆?#38171;?#28751;?#37824;惧
return false;
}
if (exception instanceof NoHttpResponseException) {// 濡傛灉鏈嶅姟鍣?#28051;帀浜?#26473;炴帴锛?#38317;箞灏?#38322;?#29831;?br /> return true;
}
if (exception instanceof SSLHandshakeException) {// 涓?#29781;?#38322;?#29831;SSL鎻?#37813;嬪紓甯?br /> return false;
}
if (exception instanceof InterruptedIOException) {// 瓒?#37827;?br /> return false;
}
if (exception instanceof UnknownHostException) {// 鐩?#37837;?#37832;嶅姟鍣?#28051;嶅彲杈?br /> return false;
}
if (exception instanceof ConnectTimeoutException) {// 杩炴帴琚?#37815;?#32513;?br /> return false;
}
if (exception instanceof SSLException) {// ssl鎻?#37813;嬪紓甯?br /> return false;
}

HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 濡傛灉璇?#23033;傛槸骞傜瓑鐨?#38171;?#28751;卞?#23942;灏濊瘯
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
return httpRequestRetryHandler;
}



//鍒?#23524;HostnameVerifier
//鐢?#27996;?#29785;?#37712;javax.net.ssl.SSLException: hostname in certificate didn't match: <123.125.97.66> != <123.125.97.241>
static HostnameVerifier hostnameVerifier = new NoopHostnameVerifier(){
@Override
public boolean verify(String s, SSLSession sslSession) {
return super.verify(s, sslSession);
}
};


public static class PoolManager {
public static PoolingHttpClientConnectionManager clientConnectionManager = null;
private static int maxTotal = 200;
private static int defaultMaxPerRoute = 100;

private PoolManager(){
clientConnectionManager.setMaxTotal(maxTotal);
clientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
}

private static class PoolManagerHolder{
public static PoolManager instance = new PoolManager();
}

public static PoolManager getInstance() {
if(null == clientConnectionManager)
clientConnectionManager = new PoolingHttpClientConnectionManager();
return PoolManagerHolder.instance;
}

public static PoolingHttpClientConnectionManager getHttpPoolInstance() {
PoolManager.getInstance();
// System.out.println("getAvailable=" + clientConnectionManager.getTotalStats().getAvailable());
// System.out.println("getLeased=" + clientConnectionManager.getTotalStats().getLeased());
// System.out.println("getMax=" + clientConnectionManager.getTotalStats().getMax());
// System.out.println("getPending="+clientConnectionManager.getTotalStats().getPending());
return PoolManager.clientConnectionManager;
}


}

public static class PoolsManager {
public static PoolingHttpClientConnectionManager clientConnectionManager = null;
private static int maxTotal = 200;
private static int defaultMaxPerRoute = 100;

private PoolsManager(){
clientConnectionManager.setMaxTotal(maxTotal);
clientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
}

private static class PoolsManagerHolder{
public static PoolsManager instance = new PoolsManager();
}

public static PoolsManager getInstance(Registry<ConnectionSocketFactory> socketFactoryRegistry) {
if(null == clientConnectionManager)
clientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
return PoolsManagerHolder.instance;
}

public static PoolingHttpClientConnectionManager getHttpsPoolInstance(Registry<ConnectionSocketFactory> socketFactoryRegistry) {
PoolsManager.getInstance(socketFactoryRegistry);
// System.out.println("getAvailable=" + clientConnectionManager.getTotalStats().getAvailable());
// System.out.println("getLeased=" + clientConnectionManager.getTotalStats().getLeased());
// System.out.println("getMax=" + clientConnectionManager.getTotalStats().getMax());
// System.out.println("getPending="+clientConnectionManager.getTotalStats().getPending());
return PoolsManager.clientConnectionManager;
}

}

public static String httpPost(Map<String, String> request, String httpUrl){
String result = "";
CloseableHttpClient httpClient = getHttpClient();
try {
if(MapUtils.isEmpty(request))
throw new Exception("璇?#23033;傚?#20635;暟涓嶈兘涓虹");
HttpPost httpPost = new HttpPost(httpUrl);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for(Iterator<Map.Entry<String, String>> iterator=request.entrySet().iterator(); iterator.hasNext();){
Map.Entry<String, String> entry = iterator.next();
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
System.out.println("Executing request: " + httpPost.getRequestLine());
CloseableHttpResponse response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity());
System.out.println("Executing response: "+ result);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

public static String httpPost(String json, String httpUrl, Map<String, String> headers){
String result = "";
CloseableHttpClient httpClient = getHttpClient();
try {
if(StringUtils.isBlank(json))
throw new Exception("璇?#23033;傚?#20635;暟涓嶈兘涓虹");
HttpPost httpPost = new HttpPost(httpUrl);
for(Iterator<Map.Entry<String, String>> iterator=headers.entrySet().iterator();iterator.hasNext();){
Map.Entry<String, String> entry = iterator.next();
Header header = new BasicHeader(entry.getKey(), entry.getValue());
httpPost.setHeader(header);
}
httpPost.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
System.out.println("Executing request: " + httpPost.getRequestLine());
CloseableHttpResponse response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity());
System.out.println("Executing response: "+ result);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

public static String httpGet(String httpUrl, Map<String, String> headers) {
String result = "";
CloseableHttpClient httpClient = getHttpClient();
try {
HttpGet httpGet = new HttpGet(httpUrl);
System.out.println("Executing request: " + httpGet.getRequestLine());
for(Iterator<Map.Entry<String, String>> iterator=headers.entrySet().iterator();iterator.hasNext();){
Map.Entry<String, String> entry = iterator.next();
Header header = new BasicHeader(entry.getKey(), entry.getValue());
httpGet.setHeader(header);
}
CloseableHttpResponse response = httpClient.execute(httpGet);
result = EntityUtils.toString(response.getEntity());
System.out.println("Executing response: "+ result);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}


public static String httpGet(String httpUrl) {
String result = "";
CloseableHttpClient httpClient = getHttpClient();
try {
HttpGet httpGet = new HttpGet(httpUrl);
System.out.println("Executing request: " + httpGet.getRequestLine());
CloseableHttpResponse response = httpClient.execute(httpGet);
result = EntityUtils.toString(response.getEntity());
System.out.println("Executing response: "+ result);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}





maven渚濊禆锛?br />
  <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>


abin 2016-04-27 19:04 ?#24386;琛?#29831;勮
]]>
Redis 浠悊鏈嶅姟Twemproxy http://www.5432034.com/stevenjohn/archive/2015/11/03/428041.htmlabinabinTue, 03 Nov 2015 11:30:00 GMThttp://www.5432034.com/stevenjohn/archive/2015/11/03/428041.htmlhttp://www.5432034.com/stevenjohn/comments/428041.htmlhttp://www.5432034.com/stevenjohn/archive/2015/11/03/428041.html#Feedback0http://www.5432034.com/stevenjohn/comments/commentRss/428041.htmlhttp://www.5432034.com/stevenjohn/services/trackbacks/428041.html

1銆乼wemproxy explore

      褰撴垜浠?#37832;夊閲?Redis 鎴?Memcached 鐨?#37827;?#37706;?#38171;?#38315;?#29999;?#37721;兘閫?#26473;?#28729;?#37812;?#32468;?#37928;?#28051;?#31802;鏁版嵁?#22414;閰?#32480;楁硶锛堟瘮濡備竴鑷?#37804;?#37725;?#29999;?#38171;?#38171;?/span>鏉?#28729;炵幇闆嗙兢瀛樺偍鐨勭壒鎬?#37510;?/span>铏界劧Redis 2.6鐗?#37832;?#23480;?#32513;忓?#25117;竷Redis Cluster锛?#28003;?#26473;?#28051;嶆槸寰?#37812;愮?#29116;?#37922;?#23005;紡鐢?#27996;?#37916;?#28583;?#37510;?span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"> Redis 鐨?Cluster 鏂规杩?#23052;?#37832;夋?#32033;鎺?#37713;轰?#23338;墠锛屾垜浠?span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">閫?#26473;? Proxy 鐨?#37826;瑰紡鏉?#28729;炵幇闆嗙兢瀛樺偍銆?/span>

       Twitter锛?#28051;栫晫鏈澶?#37928;Redis闆嗙兢?#31651;涓閮讲?#28266;Twitter鐢?#27996;?#28051;?#37922;?#37812;?#37819;愪緵鏃堕棿杞?#37825;版嵁銆俆witter Open Source閮?#38338;?#37819;愪緵浜員wemproxy銆?/span>

     Twemproxy,涔?#37721;nutcraker銆傛槸涓涓猼wtter寮婧?#37928;?#28051;涓猺edis?#25328;memcache浠悊鏈嶅姟鍣?#37510;?redis浣?#28051;?#28051;涓?#26946;?#37825;?#37928;勭紦瀛?#37832;嶅姟鍣?#38171;?#38344;?#29999;?#37711;?#37832;?#25652;?#37922;?#28000;峰?#37510;備?#21976;槸褰?#28003;?#37922;瘮杈冨鐨?#37827;?#37706;?#38171;?#28751;?#29999;?#37832;?#37721;?#28000;?#38315;?#26473;?#37836;愮鏂瑰紡 缁?#28051;杩?/span>琛?#32480;悊銆?#38316;垮?#23942;瘡涓?#25652;?#37922;瘡涓?#28729;?#37812;?#32468;?#32480;悊杩炴帴鐨?#37833;?#37825;?#37804;?#37510;?#37722;?#37827;跺湪涓瀹氱?#23338;害涓婂?#27194;緱鍙?#28000;?#37818;?#37714;?#37510;?/p>

      Twemproxy?#27128;涓涓?#36423;?#38315;?#37928;勫崟绾?#32459;?#28000;悊绋嬪簭锛?#37824;?#37816;Memcached ASCII鍗?#29825;?#37724;屾洿鏂?#37928;Redis鍗?#29825;?#38171;?/span>

     瀹?#37711;?#38318;?#37922;C鍐欐垚锛?#28003;?#37922;Apache 2.0 License鎺?#37833;?#37510;?#26916;?#37929;湪Linux涓婂彲浠?#23480;?#28003;?#38171;?#38000;屽湪OSX涓?#37827;犳硶缂?#29831;?#38171;屽洜涓?#28729;?#28186;濊禆浜epoll API.

      Twemproxy 閫?#26473;囧紩鍏?#28051;涓?#28000;?#37918;嗗眰锛屽彲浠?#28751;?#37711;?#37722;?#32468;?#37928;勫?#27692;彴 Redis 鎴?Memcached 瀹炰緥杩?#29723;?#32513;?#28051;绠悊涓庡垎閰?#38171;?#28003;垮簲鐢?#32459;嬪?#24531;彧?#28214;瑕佸湪 Twemproxy 涓?#26473;?#29723;屾搷浣?#38171;?#38000;?#28051;?#37922;?#37711;?#36423;冨悗闈?#37711;?#28003;?#37832;夊灏?#28051;?#37930;?#28729;?#37928;?Redis 鎴?Memcached 瀛樺偍銆?/span> 

2銆?/span>twemproxy?#22738;鎬?#38171;?/span>

    • 鏀?#37816;?#28598;?#29840;?#38010;?#37904;?#38007;?#37716;?#37714;犻櫎

      • 鍙?#28000;?#29825;剧疆閲?#37826;?#26473;炴帴璇?#38010;?#37904;?#37928;?#37827;堕棿
      • 鍙?#28000;?#29825;剧疆杩炴帴澶?#28751;戞涔嬪悗鍒犻櫎璇?#38010;?#37904;?/li>
      • 璇?#37826;瑰紡閫?#37722;?#28003;?#28051;cache瀛樺偍
    • 鏀?#37816;佽?#21095;?#29950;ashTag

      • 閫?#26473;HashTag鍙?#28000;?#38007;繁璁?#28729;?#28751;?#28051;?#28051;狵EYhash?#22476;鍚?#28051;涓?#28729;炰緥涓婂幓銆?/li>
    • 鍑?#28751;?#28051;巖edis鐨勭洿?#24116;杩炴帴鏁?/p>

      • 淇?#37816;?#28051;巖edis鐨勯暱杩炴帴
      • 鍙?#29825;剧疆浠悊涓?#37722;?#37721;版瘡涓猺edis杩炴帴鐨?#37825;?#37929;?/li>
    • 鑷?#37716;垎鐗囧埌鍚?#32468;?#28598;?#28051;猺edis瀹炰緥涓?/p>

      • 澶氱hash绠楁硶锛?span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25px">?#20824;澶?#28003;?#37922;?#28051;嶅悓鐨勭?#26667;暐鍜屾暎鍒?#37713;?#37825;?#37824;?#37816;?#28051;鑷?#37804;hash銆?/span>
      • 鍙?#28000;?#29825;剧疆鍚?#32468;?#28729;炰緥鐨?#37833;?#38322;?/li>
    • 閬垮?#23941;崟鐐归棶棰?/p>

      • 鍙?#28000;?#39582;?#29723;?#38318;讲澶?#28051;?#28000;?#37918;嗗眰.client鑷?#37716;?#38315;夋嫨鍙?#37922;?#37928;?#28051;涓?/li>
    • 鏀?#37816;redis pipelining request

           鏀?#37816;佽姹?#37928;勬?#20344;紡涓?#37813;瑰?#21229;悊锛?#38340;?#28003;?#37833;?#37733;?#37928;勬秷鑰?/span>

    • 鏀?#37816;佺?#33333;?#37929;戞

      • 鍙?#29825;剧疆鐘舵?#37929;戞ip?#25328;绔?#37721;?#38171;?#29825;块棶ip?#25328;绔?#37721;?#37721;?#28000;?#23536;楀埌涓涓猨son鏍煎紡鐨勭?#33333;?#28103;?#37805;?#28051;?/li>
      • 鍙?#29825;剧疆鐩戞淇?#37805;埛鏂伴棿闅?#37827;堕棿
    • 楂樺悶鍚?#38322;?/p>

      • 杩炴帴澶?#37922;?#38171;?#37712;?#28699;樺鐢?#37510;?/li>
      • 灏嗗涓?#26473;炴帴璇?#23033;?#38171;?#32513;?#37812;reids pipelining缁?#28051;鍚憆edis璇?#23033;?#37510;?/li>

     鍙?#28598;?#37721;?#28000;?#28103;?#37824;redis鐨勬?#24874;唬鐮?#38171;屾娊鍙?#37713;簉edis涓?#37928;勫?#23941;崐閮垎锛?#28003;?#28051;?#28051;涓?#28051;棿浠?#37918;嗗眰銆?#37832;缁?#38318;芥槸閫?#26473;linux涓?#37928;epoll 浜?#28000;?#37832;哄?#33333;彁楂樺苟?#24386;鏁?#37916;?#38171;?#37711;?#28051;璶utcraker鏈韩涔熸槸浣?#37922;epoll鐨?#27996;?#28000;?#37832;哄埗銆傚苟涓斿湪鎬兘娴?#29831;?#28051;?#37928;勮?#24135;闈?#29999;?#37713;鸿壊銆?/p>

3銆乼wemproxy?#26870;棰?#28051;?#28051;嶈


Twemproxy 鐢?#27996;?#37711;?#38007;韩鍘熺悊闄愬埗锛?#37832;?#28051;?#31802;涓嶈涔嬪锛?#28641;?#38171;?nbsp;
  • 涓?#37824;?#37816;?#38333;?#28725;瑰涓?#37706;?#37928;勬搷浣?#38171;屾瘮濡傚彇sets鐨勫瓙浜苟琛瓑锛圡GET ?#25328; DEL ?#27342;澶?#38171;?/span>
  • 涓?#37824;?#37816;丷edis鐨?#27996;嬪姟鎿?#28003;?/span>
  • 鍑?#38335;欐彁绀?#26473;?#28051;嶅瀹屽
  • 涔?#28051;?#37824;?#37816;乻elect鎿?#28003;?/li>

4銆?#28729;夎涓?#38320;嶇疆 

鍏?#28003;?#37928;勫?#22798;?#21614;楠?#37721;?#37922;?#37836;湅github锛?a href="https://github.com/twitter/twemproxy" target="_blank">https://github.com/twitter/twemproxy
Twemproxy 鐨勫?#22798;锛?#28051;?#29781;?#37723;?#28000;?#28641;備笅锛?nbsp;
apt-get install automake  
apt-get install libtool  
git clone git://github.com/twitter/twemproxy.git  
cd twemproxy  
autoreconf -fvi  
./configure --enable-debug=log  
make  
src/nutcracker -h

閫?#26473;?#28051;?#38344;?#37928;勫懡浠?#28751;?#32480;?#28729;夎?#21613;浜?#38171;岀劧鍚庢槸鍏?#28003;?#37928;勯?#23943;疆锛?#28051;?#38344;槸涓涓?#37711;稿瀷鐨勯?#23943;疆 
    redis1:  
      listen: 127.0.0.1:6379 #浣?#37922;?#37725;?#28051;?#32468;?#37721;?#37722;?#37716;Twemproxy  
      redis: true #?#27128;鍚槸Redis鐨刾roxy  
      hash: fnv1a_64 #鎸?#28729;?#37711;?#28003;?#37928;刪ash鍑?#37825;? 
      distribution: ketama #鍏?#28003;?#37928;刪ash绠楁硶  
      auto_eject_hosts: true #?#27128;鍚湪缁?#37904;?#37827;?#23049;曞?#23941;簲鐨?#37827;?#37706;?#28051;?#37827;?#37821;橀櫎缁?#37904;? 
      timeout: 400 #瓒?#37827;?#37827;堕棿锛?#23011;?#32457;?#38171;? 
      server_retry_timeout: 2000 #閲?#29831;?#37928;?#37827;堕棿锛?#23011;?#32457;?#38171;? 
      server_failure_limit: 1 #缁?#37904;?#37825;?#38341;?#28598;?#28751;戞灏?#32480;?#37821;橀櫎?#24064;  
      servers: #涓?#38344;?#29723;?#32448;?#37813;鏈?#37928;Redis鑺?#37904;?#38171;圛P:绔?#37721;?#37721;?鏉?#38322;?#38171;? 
       - 127.0.0.1:6380:1  
       - 127.0.0.1:6381:1  
       - 127.0.0.1:6382:1  
      
    redis2:  
      listen: 0.0.0.0:10000  
      redis: true  
      hash: fnv1a_64  
      distribution: ketama  
      auto_eject_hosts: false  
      timeout: 400  
      servers:  
       - 127.0.0.1:6379:1  
       - 127.0.0.1:6380:1  
       - 127.0.0.1:6381:1  
       - 127.0.0.1:6382:1 

浣?#37721;?#28000;?#37722;?#37827;?#23534;鍚?#28598;?#28051;?Twemproxy 瀹炰緥锛?#28729;?#28000;?#38318;?#37721;?#28000;?#26473;?#29723;?#29831;?#37712;?#38171;?#26473;欐牱浣?#37928;勫簲鐢?#32459;嬪簭灏卞彲浠?#28729;?#37711;?#38316;垮厤鎵璋?#37928;勫崟鐐?#37825;?#38341;?#37510;?


http://blog.csdn.net/hguisu/article/details/9174459/


abin 2015-11-03 19:30 ?#24386;琛?#29831;勮
]]>
11ѡ5ʱʱ
  1. <mark id="ghavm"><button id="ghavm"></button></mark>

      <listing id="ghavm"><strong id="ghavm"></strong></listing>
      <code id="ghavm"></code><rt id="ghavm"><label id="ghavm"><legend id="ghavm"></legend></label></rt>
      1. <tt id="ghavm"><button id="ghavm"></button></tt>

        1. <mark id="ghavm"><button id="ghavm"></button></mark>

            <listing id="ghavm"><strong id="ghavm"></strong></listing>
            <code id="ghavm"></code><rt id="ghavm"><label id="ghavm"><legend id="ghavm"></legend></label></rt>
            1. <tt id="ghavm"><button id="ghavm"></button></tt>