锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲乱码中文字幕小综合,日韩亚洲人成在线,亚洲国产精品一区二区第四页http://m.tkk7.com/stevenjohn/閭d簺闈掓槬鐨勫瞾鏈?/description>zh-cnThu, 27 Mar 2025 09:33:16 GMTThu, 27 Mar 2025 09:33:16 GMT60濡備綍鐢ㄦ秷鎭郴緇熼伩鍏嶅垎甯冨紡浜嬪姟錛?/title><link>http://m.tkk7.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://m.tkk7.com/stevenjohn/archive/2018/01/04/433004.html</guid><wfw:comment>http://m.tkk7.com/stevenjohn/comments/433004.html</wfw:comment><comments>http://m.tkk7.com/stevenjohn/archive/2018/01/04/433004.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/stevenjohn/comments/commentRss/433004.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/stevenjohn/services/trackbacks/433004.html</trackback:ping><description><![CDATA[     鎽樿: 鍓嶉樀瀛愪粠鏀粯瀹濊漿璐?涓囧潡閽卞埌浣欓瀹濓紝榪欐槸鏃ュ父鐢熸椿鐨勪竴浠舵櫘閫氬皬浜嬶紝浣嗕綔涓轟簰鑱旂綉鐮斿彂浜哄憳鐨勮亴涓氱梾錛屾垜灝辨濊冩敮浠樺疂鎵i櫎1涓囦箣鍚庯紝濡傛灉緋葷粺鎸傛帀鎬庝箞鍔烇紝榪欐椂浣欓瀹濊處鎴峰茍娌℃湁澧炲姞1涓囷紝鏁版嵁灝變細鍑虹幇涓嶄竴鑷寸姸鍐典簡銆備笂榪板満鏅湪鍚勪釜綾誨瀷鐨勭郴緇熶腑閮借兘鎵懼埌鐩鎬技褰卞瓙錛屾瘮濡傚湪鐢靛晢緋葷粺涓紝褰撴湁鐢ㄦ埛涓嬪崟鍚庯紝闄や簡鍦ㄨ鍗曡〃鎻掑叆涓鏉¤褰曞錛屽搴斿晢鍝佽〃鐨勮繖涓晢鍝佹暟閲忓繀欏誨噺1鍚э紝鎬庝箞淇濊瘉錛燂紒鍦ㄦ悳绱㈠箍鍛婄郴緇熶腑錛屽綋鐢ㄦ埛鐐瑰嚮鏌愬箍鍛婂悗錛岄櫎浜嗗湪鐐瑰嚮...  <a href='http://m.tkk7.com/stevenjohn/archive/2018/01/04/433004.html'>闃呰鍏ㄦ枃</a><img src ="http://m.tkk7.com/stevenjohn/aggbug/433004.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/stevenjohn/" target="_blank">abin</a> 2018-01-04 00:01 <a href="http://m.tkk7.com/stevenjohn/archive/2018/01/04/433004.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>寰湇鍔′紭鐐圭己鐐?/title><link>http://m.tkk7.com/stevenjohn/archive/2017/12/31/432996.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sun, 31 Dec 2017 08:41:00 GMT</pubDate><guid>http://m.tkk7.com/stevenjohn/archive/2017/12/31/432996.html</guid><wfw:comment>http://m.tkk7.com/stevenjohn/comments/432996.html</wfw:comment><comments>http://m.tkk7.com/stevenjohn/archive/2017/12/31/432996.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/stevenjohn/comments/commentRss/432996.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/stevenjohn/services/trackbacks/432996.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: #454545; background-color: #ffffff;">寰湇鍔℃灦鏋勯噰鐢⊿cale Cube鏂規硶璁捐搴旂敤鏋舵瀯錛屽皢搴旂敤鏈嶅姟鎸夊姛鑳芥媶鍒嗘垚涓緇勭浉浜掑崗浣滅殑鏈嶅姟銆傛瘡涓湇鍔¤礋璐d竴緇勭壒瀹氥佺浉鍏崇殑鍔熻兘銆傛瘡涓湇鍔″彲浠ユ湁鑷繁鐙珛鐨勬暟鎹簱錛屼粠鑰屼繚璇佷笌鍏朵粬鏈嶅姟瑙h︺?/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-weight: bold;">寰湇鍔′紭鐐?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">1銆?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">閫氳繃鍒嗚В宸ㄥぇ鍗曚綋寮忓簲鐢ㄤ負澶氫釜鏈嶅姟鏂規硶瑙e喅浜嗗鏉傛ч棶棰橈紝</span><span style="font-size: 16px; color: #454545; background-color: #ffffff;">姣忎釜寰湇鍔$浉瀵硅緝灝?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">2銆佹瘡涓崟浣撳簲鐢ㄤ笉灞闄愪簬鍥哄畾鐨勬妧鏈爤錛?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">寮鍙戣呭彲浠ヨ嚜鐢遍夋嫨寮鍙戞妧鏈紝鎻愪緵API鏈嶅姟銆?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">3銆?span style="font-family: SimHei, STHeiti; color: #666666; background-color: #ffffff;">姣忎釜寰湇鍔$嫭绔嬬殑寮鍙戯紝閮ㄧ講</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">4銆佸崟涓鑱岃矗鍔熻兘錛?span style="font-family: Arial; background-color: #ffffff;">姣忎釜鏈嶅姟閮藉緢綆鍗曪紝鍙叧娉ㄤ簬涓涓笟鍔″姛鑳?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;">5銆?span style="font-size: 16px; color: #454545; background-color: #ffffff;">鏄撲簬瑙勬ā鍖栧紑鍙戯紝澶氫釜寮鍙戝洟闃熷彲浠ュ茍琛屽紑鍙戯紝姣忎釜鍥㈤槦璐熻矗涓欏規湇鍔?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">6銆佹敼鍠勬晠闅滈殧紱匯備竴涓湇鍔″畷鏈轟笉浼氬獎鍝嶅叾浠栫殑鏈嶅姟</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;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="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: #454545; background-color: #ffffff;">1.寮鍙戣呴渶瑕佸簲瀵瑰垱寤哄垎甯冨紡緋葷粺鎵浜х敓鐨勯澶栫殑澶嶆潅鍥犵礌</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">l  鐩墠鐨処DE涓昏闈㈠鐨勬槸鍗曚綋宸ョ▼紼嬪簭錛屾棤娉曟樉紺烘敮鎸佸垎甯冨紡搴旂敤鐨勫紑鍙?/span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">l  嫻嬭瘯宸ヤ綔鏇村姞鍥伴毦</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">l  闇瑕侀噰鐢ㄦ湇鍔¢棿鐨勯氳鏈哄埗</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">l  寰堥毦鍦ㄤ笉閲囩敤鍒嗗竷寮忎簨鍔$殑鎯呭喌涓嬭法鏈嶅姟瀹炵幇鍔熻兘</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">l  璺ㄦ湇鍔″疄鐜拌姹傚姛鑳借姹傚洟闃熶箣闂寸殑绱у瘑鍗忎綔</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">2.閮ㄧ講澶嶆潅</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 16px; color: #454545; background-color: #ffffff;">3.鍐呭瓨鍗犵敤閲忔洿楂?/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;"></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><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><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><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><img src ="http://m.tkk7.com/stevenjohn/aggbug/432996.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/stevenjohn/" target="_blank">abin</a> 2017-12-31 16:41 <a href="http://m.tkk7.com/stevenjohn/archive/2017/12/31/432996.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>JDK 婧愮爜涓?HashMap 鐨?hash 鏂規硶鍘熺悊鏄粈涔堬紵http://m.tkk7.com/stevenjohn/archive/2017/12/24/432975.htmlabinabinSun, 24 Dec 2017 14:38:00 GMThttp://m.tkk7.com/stevenjohn/archive/2017/12/24/432975.htmlhttp://m.tkk7.com/stevenjohn/comments/432975.htmlhttp://m.tkk7.com/stevenjohn/archive/2017/12/24/432975.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/432975.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/432975.htmlJDK 鐨?HashMap 涓嬌鐢ㄤ簡涓涓?hash 鏂規硶鏉ュ仛 bit shifting錛屽湪娉ㄩ噴涓鏄庢槸涓轟簡闃叉涓浜涘疄鐜版瘮杈冨樊鐨刪ashCode() 鏂規硶錛岃闂師鐞嗘槸浠涔堬紵JDK 鐨勬簮鐮佸弬瑙侊細GrepCode: java.util.HashMap (.java)
/**
 * Applies a supplemental hash function to a given hashCode, which
 * defends against poor quality hash functions.  This is critical
 * because HashMap uses power-of-two length hash tables, that
 * otherwise encounter collisions for hashCodes that do not differ
 * in lower bits. Note: Null keys always map to hash 0, thus index 0.
 */
static int hash(int h) {
    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);
}
PS錛氱綉涓婄湅瑙佹湁浜鴻浣滆呮湰浜鴻鍘熺悊闇瑕佸弬瑙佸湥緇忋婅綆楁満紼嬪簭璁捐鑹烘湳銆嬬殑 Vol.3 閲屽ご鐨勪粙緇嶏紝涓嶈繃鏈ㄦ湁鐪嬭繃紲炰功錛屾眰杈句漢浠嬬粛





榪欐浠g爜鍙?#8220;鎵板姩鍑芥暟”銆?br />棰樹富璐寸殑鏄疛ava 7鐨凥ashMap鐨勬簮鐮侊紝Java 8涓繖姝ュ凡緇忕畝鍖栦簡錛屽彧鍋氫竴嬈?6浣嶅彸浣嶇Щ寮傛垨娣峰悎錛岃屼笉鏄洓嬈★紝浣嗗師鐞嗘槸涓嶅彉鐨勩備笅闈互Java 8鐨勬簮鐮佷負渚嬭В閲婏紝

//Java 8涓殑鏁e垪鍊間紭鍖栧嚱鏁皊taticfinalinthash(Objectkey){inth;return(key==null)?0:(h=key.hashCode())^(h>>>16);//key.hashCode()涓哄搱甯岀畻娉曪紝榪斿洖鍒濆鍝堝笇鍊紏
澶у閮界煡閬撲笂闈唬鐮侀噷鐨刱ey.hashCode()鍑芥暟璋冪敤鐨勬槸key閿肩被鍨嬭嚜甯︾殑鍝堝笇鍑芥暟錛岃繑鍥瀒nt鍨嬫暎鍒楀箋傜悊璁轟笂鏁e垪鍊兼槸涓涓猧nt鍨嬶紝濡傛灉鐩存帴鎷挎暎鍒楀間綔涓轟笅鏍囪闂瓾ashMap涓繪暟緇勭殑璇濓紝鑰冭檻鍒?榪涘埗32浣嶅甫絎﹀彿鐨刬nt琛ㄥ艱寖鍥翠粠-2147483648鍒?147483648銆傚墠鍚庡姞璧鋒潵澶ф40浜跨殑鏄犲皠絀洪棿銆傚彧瑕佸搱甯屽嚱鏁版槧灝勫緱姣旇緝鍧囧寑鏉炬暎錛屼竴鑸簲鐢ㄦ槸寰堥毦鍑虹幇紕版挒鐨勩備絾闂鏄竴涓?0浜塊暱搴︾殑鏁扮粍錛屽唴瀛樻槸鏀句笉涓嬬殑銆備綘鎯籌紝HashMap鎵╁涔嬪墠鐨勬暟緇勫垵濮嬪ぇ灝忔墠16銆傛墍浠ヨ繖涓暎鍒楀兼槸涓嶈兘鐩存帴鎷挎潵鐢ㄧ殑銆傜敤涔嬪墠榪樿鍏堝仛瀵規暟緇勭殑闀垮害鍙栨ā榪愮畻錛屽緱鍒扮殑浣欐暟鎵嶈兘鐢ㄦ潵璁塊棶鏁扮粍涓嬫爣銆傛簮鐮佷腑妯¤繍綆楁槸鍦ㄨ繖涓猧ndexFor( )鍑芥暟閲屽畬鎴愮殑銆?br />
bucketIndex = indexFor(hash, table.length);indexFor鐨勪唬鐮佷篃寰堢畝鍗曪紝灝辨槸鎶婃暎鍒楀煎拰鏁扮粍闀垮害鍋氫竴涓?涓?鎿嶄綔錛?br />
static int indexFor(int h, int length) {        return h & (length-1);}欏轟究璇翠竴涓嬶紝榪欎篃姝eソ瑙i噴浜嗕負浠涔圚ashMap鐨勬暟緇勯暱搴﹁鍙?鐨勬暣嬈″箓銆傚洜涓鴻繖鏍鳳紙鏁扮粍闀垮害-1錛夋濂界浉褰撲簬涓涓?#8220;浣庝綅鎺╃爜”銆?#8220;涓?#8221;鎿嶄綔鐨勭粨鏋滃氨鏄暎鍒楀肩殑楂樹綅鍏ㄩ儴褰掗浂錛屽彧淇濈暀浣庝綅鍊鹼紝鐢ㄦ潵鍋氭暟緇勪笅鏍囪闂備互鍒濆闀垮害16涓轟緥錛?6-1=15銆?榪涘埗琛ㄧず鏄?0000000 00000000 00001111銆傚拰鏌愭暎鍒楀煎仛“涓?#8221;鎿嶄綔濡備笅錛岀粨鏋滃氨鏄埅鍙栦簡鏈浣庣殑鍥涗綅鍊箋?br />10100101 11000100 00100101& 00000000 00000000 00001111---------------------------------- 00000000 00000000 00000101    //楂樹綅鍏ㄩ儴褰掗浂錛屽彧淇濈暀鏈洓浣?br />浣嗚繖鏃跺欓棶棰樺氨鏉ヤ簡錛岃繖鏍峰氨綆楁垜鐨勬暎鍒楀煎垎甯冨啀鏉炬暎錛岃鏄彧鍙栨渶鍚庡嚑浣嶇殑璇濓紝紕版挒涔熶細寰堜弗閲嶃傛洿瑕佸懡鐨勬槸濡傛灉鏁e垪鏈韓鍋氬緱涓嶅ソ錛屽垎甯冧笂鎴愮瓑宸暟鍒楃殑婕忔礊錛屾伆濂戒嬌鏈鍚庡嚑涓綆浣嶅憟鐜拌寰嬫ч噸澶嶏紝灝辨棤姣旇泲鐤箋傝繖鏃跺?#8220;鎵板姩鍑芥暟”鐨勪環鍊煎氨浣撶幇鍑烘潵浜嗭紝璇村埌榪欓噷澶у搴旇鐚滃嚭鏉ヤ簡銆傜湅涓嬮潰榪欎釜鍥撅紝


鍙充綅縐?6浣嶏紝姝eソ鏄?2bit鐨勪竴鍗婏紝鑷繁鐨勯珮鍗婂尯鍜屼綆鍗婂尯鍋氬紓鎴栵紝灝辨槸涓轟簡娣峰悎鍘熷鍝堝笇鐮佺殑楂樹綅鍜屼綆浣嶏紝浠ユ鏉ュ姞澶т綆浣嶇殑闅忔満鎬с傝屼笖娣峰悎鍚庣殑浣庝綅鎺烘潅浜嗛珮浣嶇殑閮ㄥ垎鐗瑰緛錛岃繖鏍烽珮浣嶇殑淇℃伅涔熻鍙樼浉淇濈暀涓嬫潵銆傛渶鍚庢垜浠潵鐪嬩竴涓婸eterLawley鐨勪竴綃囦笓鏍忔枃绔犮夾n introduction to optimising a hashing strategy銆嬮噷鐨勭殑涓涓疄楠岋細浠栭殢鏈洪夊彇浜?52涓瓧絎︿覆錛屽湪浠栦滑鏁e垪鍊煎畬鍏ㄦ病鏈夊啿紿佺殑鍓嶆彁涓嬶紝瀵瑰畠浠仛浣庝綅鎺╃爜錛屽彇鏁扮粍涓嬫爣銆?br />

緇撴灉鏄劇ず錛屽綋HashMap鏁扮粍闀垮害涓?12鐨勬椂鍊欙紝涔熷氨鏄敤鎺╃爜鍙栦綆9浣嶇殑鏃跺欙紝鍦ㄦ病鏈夋壈鍔ㄥ嚱鏁扮殑鎯呭喌涓嬶紝鍙戠敓浜?03嬈$鎾烇紝鎺ヨ繎30%銆傝屽湪浣跨敤浜嗘壈鍔ㄥ嚱鏁頒箣鍚庡彧鏈?2嬈$鎾炪傜鎾炲噺灝戜簡灝嗚繎10%銆傜湅鏉ユ壈鍔ㄥ嚱鏁扮‘瀹炶繕鏄湁鍔熸晥鐨勩備絾鏄庢樉Java 8瑙夊緱鎵板姩鍋氫竴嬈″氨澶熶簡錛屽仛4嬈$殑璇濓紝澶氫簡鍙兘杈歸檯鏁堢敤涔熶笉澶э紝鎵璋撲負浜嗘晥鐜囪冭檻灝辨敼鎴愪竴嬈′簡銆?br />------------------------------------------------------








https://www.zhihu.com/question/20733617





abin 2017-12-24 22:38 鍙戣〃璇勮
]]>
GoLang涔嬫柟娉曚笌鎺ュ彛http://m.tkk7.com/stevenjohn/archive/2017/08/03/432720.htmlabinabinThu, 03 Aug 2017 03:34:00 GMThttp://m.tkk7.com/stevenjohn/archive/2017/08/03/432720.htmlhttp://m.tkk7.com/stevenjohn/comments/432720.htmlhttp://m.tkk7.com/stevenjohn/archive/2017/08/03/432720.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/432720.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/432720.htmlGo璇█娌℃湁娌胯浼犵粺闈㈠悜瀵硅薄緙栫▼涓殑璇稿姒傚康錛屾瘮濡傜戶鎵褲佽櫄鍑芥暟銆佹瀯閫犲嚱鏁板拰鏋愭瀯鍑芥暟銆侀殣钘忕殑this鎸囬拡絳夈?/p>

 

鏂規硶

Go 璇█涓悓鏃舵湁鍑芥暟鍜屾柟娉曘?span style="color: #ff0000;">鏂規硶灝辨槸涓涓寘鍚簡鎺ュ彈鑰咃紙receiver錛夌殑鍑芥暟錛宺eceiver鍙互鏄唴緗被鍨嬫垨鑰呯粨鏋勪綋綾誨瀷鐨勪竴涓兼垨鑰呮槸涓涓寚閽堛傛墍鏈夌粰瀹氱被鍨嬬殑鏂規硶灞炰簬璇ョ被鍨嬬殑鏂規硶闆嗐?br />

濡備笅闈㈢殑榪欎釜渚嬪瓙錛屽畾涔変簡涓涓柊綾誨瀷Integer錛屽畠鍜宨nt涓鏍鳳紝鍙槸涓哄畠鍐呯疆鐨刬nt綾誨瀷澧炲姞浜嗕釜鏂版柟娉昄ess()

澶嶅埗浠g爜
type Integer int   func (a Integer) Less(b Integer) bool {     return a < b  }  func main() {     var a Integer = 1       if a.Less(2) {         fmt.Println("less then 2")     }    }
澶嶅埗浠g爜

鍙互鐪嬪嚭錛孏o璇█鍦ㄨ嚜瀹氫箟綾誨瀷鐨勫璞′腑娌℃湁C++/Java閭g闅愯棌鐨則his鎸囬拡錛岃屾槸鍦ㄥ畾涔夋垚鍛樻柟娉曟椂鏄懼紡澹版槑浜嗗叾鎵灞炵殑瀵硅薄銆?/p>

 

method鐨勮娉曞涓嬶細

func (r ReceiverType) funcName(parameters) (results)

褰撹皟鐢╩ethod鏃訛紝浼氬皢receiver浣滀負鍑芥暟鐨勭涓涓弬鏁幫細

funcName(r, parameters);

鎵浠ワ紝receiver鏄肩被鍨嬭繕鏄寚閽堢被鍨嬭鐪媘ethod鐨勪綔鐢ㄣ傚鏋滆淇敼瀵硅薄鐨勫鹼紝灝遍渶瑕佷紶閫掑璞$殑鎸囬拡銆?/p>

鎸囬拡浣滀負Receiver浼氬瀹炰緥瀵硅薄鐨勫唴瀹瑰彂鐢熸搷浣?鑰屾櫘閫氱被鍨嬩綔涓篟eceiver浠呬粎鏄互鍓湰浣滀負鎿嶄綔瀵硅薄,騫朵笉瀵瑰師瀹炰緥瀵硅薄鍙戠敓鎿嶄綔銆?/p>

澶嶅埗浠g爜
func (a *Ingeger) Add(b Integer) {     *a += b }  func main() {     var a Integer = 1      a.Add(3)     fmt.Println("a =", a)     //  a = 4 }
澶嶅埗浠g爜

濡傛灉Add鏂規硶涓嶄嬌鐢ㄦ寚閽堬紝鍒檃榪斿洖鐨勭粨鏋滀笉鍙橈紝榪欐槸鍥犱負Go璇█鍑芥暟鐨勫弬鏁頒篃鏄熀浜庡間紶閫掋?/p>

娉ㄦ剰錛?span style="color: #ff0000;">褰撴柟娉曠殑鎺ュ彈鑰呮槸鎸囬拡鏃訛紝鍗充嬌鐢ㄥ肩被鍨嬭皟鐢ㄩ偅涔堟柟娉曞唴閮ㄤ篃鏄鎸囬拡鐨勬搷浣溿?/span>

 

涔嬪墠璇磋繃錛孏o璇█娌℃湁鏋勯犲嚱鏁扮殑姒傚康錛岄氬父浣跨敤涓涓叏灞鍑芥暟鏉ュ畬鎴愩備緥濡傦細

澶嶅埗浠g爜
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) }
澶嶅埗浠g爜

 

 


鍖垮悕緇勫悎

Go璇█鎻愪緵浜嗙戶鎵匡紝浣嗘槸閲囩敤浜嗙粍鍚堢殑璇硶錛屾垜浠皢鍏剁О涓哄尶鍚嶇粍鍚堬紝渚嬪錛?/span>

澶嶅埗浠g爜
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 int  }  func (derived *Derived) Get() (nm string, ag int) {     return derived.name, derived.age }   func main() {     b := &Derived{}      b.Set("sina")     fmt.Println(b.Get()) }
澶嶅埗浠g爜

渚嬪瓙涓紝鍦˙ase綾誨瀷瀹氫箟浜唃et()鍜宻et()涓や釜鏂規硶錛岃孌erived綾誨瀷緇ф壙浜咮ase綾伙紝騫舵敼鍐欎簡Get()鏂規硶錛屽湪Derived瀵硅薄璋冪敤Set()鏂規硶錛屼細鍔犺澆鍩虹被瀵瑰簲鐨勬柟娉曪紱鑰岃皟鐢℅et()鏂規硶鏃訛紝鍔犺澆媧劇敓綾繪敼鍐欑殑鏂規硶銆?/p>

 

緇勫悎鐨勭被鍨嬪拰琚粍鍚堢殑綾誨瀷鍖呭惈鍚屽悕鎴愬憳鏃訛紝 浼氫笉浼氭湁闂鍛紵鍙互鍙傝冧笅闈㈢殑渚嬪瓙錛?/p>

澶嶅埗浠g爜
type Base struct {     name string     age int }  func (base *Base) Set(myname string, myage int) {     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) }
澶嶅埗浠g爜

 

 

 


鍊艱涔夊拰寮曠敤璇箟

鍊艱涔夊拰寮曠敤璇箟鐨勫樊鍒湪浜庤祴鍊鹼紝姣斿

b = a b.Modify()

濡傛灉b鐨勪慨鏀逛笉浼氬獎鍝峚鐨勫鹼紝閭d箞姝ょ被鍨嬪睘浜庡肩被鍨嬶紱濡傛灉浼氬獎鍝峚鐨勫鹼紝閭d箞姝ょ被鍨嬫槸寮曠敤綾誨瀷銆?/span>

Go璇█涓殑澶у鏁扮被鍨嬮兘鍩轟簬鍊艱涔夛紝鍖呮嫭錛?/p>

  • 鍩烘湰綾誨瀷錛屽byte銆乮nt銆乥ool銆乫loat32銆乻tring絳夛紱
  • 澶嶅悎綾誨瀷錛屽arry銆乻truct銆乸ointer絳夛紱

 

C璇█涓殑鏁扮粍姣旇緝鐗瑰埆錛岄氳繃鍑芥暟浼犻掍竴涓暟緇勭殑鏃跺欏熀浜庡紩鐢ㄨ涔夛紝浣嗘槸鍦ㄧ粨鏋勪綋瀹氫箟鏁扮粍鍙橀噺鐨勬椂鍊欏熀浜庡艱涔夈傝屽湪Go璇█涓紝鏁扮粍鍜屽熀鏈被鍨嬫病鏈夊尯鍒紝鏄緢綰補鐨勫肩被鍨嬶紝渚嬪錛?/p>

var a = [3] int{1,2,3} var b = a b[1]++ fmt.Println(a, b)   // [1 2 3] [1 3 3]

浠庣粨鏋滅湅錛宐=a璧嬪艱鍙ユ槸鏁扮粍鍐呭鐨勫畬鏁村鍒訛紝瑕佹兂琛ㄨ揪寮曠敤錛岄渶瑕佺敤鎸囬拡錛?/p>

var a = [3] int{1,2,3} var b = &a銆銆銆銆// 寮曠敤璇箟 b[1]++ fmt.Println(a, b)   // [1 3 3] [1 3 3]

 

 


鎺ュ彛

Interface 鏄竴緇勬娊璞℃柟娉曪紙鏈叿浣撳疄鐜扮殑鏂規硶/浠呭寘鍚柟娉曞悕鍙傛暟榪斿洖鍊肩殑鏂規硶錛夌殑闆嗗悎錛屽鏋滃疄鐜頒簡 interface 涓殑鎵鏈夋柟娉曪紝鍗寵綾?瀵硅薄灝卞疄鐜頒簡璇ユ帴鍙c?/p>

Interface 鐨勫0鏄庢牸寮忥細

type interfaceName interface {       //鏂規硶鍒楄〃   }  

Interface 鍙互琚換鎰忓璞″疄鐜幫紝涓涓被鍨?瀵硅薄涔熷彲浠ュ疄鐜板涓?interface錛?br />interface鐨勫彉閲忓彲浠ユ寔鏈変換鎰忓疄鐜拌interface綾誨瀷鐨勫璞°?/span>

 濡備笅闈㈢殑渚嬪瓙錛?/p>

澶嶅埗浠g爜
package main      import "fmt"      type Human struct {         name string         age int         phone string     }      type Student struct {         Human //鍖垮悕瀛楁         school string         loan float32     }      type Employee struct {         Human //鍖垮悕瀛楁         company string         money float32     }      //Human瀹炵幇SayHi鏂規硶     func (h Human) SayHi() {         fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)     }      //Human瀹炵幇Sing鏂規硶     func (h Human) Sing(lyrics string) {         fmt.Println("La la la la...", lyrics)     }      //Employee閲嶈澆Human鐨凷ayHi鏂規硶     func (e Employee) SayHi() {         fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,             e.company, e.phone)         }      // Interface Men琚獺uman,Student鍜孍mployee瀹炵幇     // 鍥犱負榪欎笁涓被鍨嬮兘瀹炵幇浜嗚繖涓や釜鏂規硶     type Men interface {         SayHi()         Sing(lyrics string)     }      func main() {         mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}         paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}         sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}         tom := Employee{Human{"Tom", 37, "222-444-XXX"}, "Things Ltd.", 5000}          //瀹氫箟Men綾誨瀷鐨勫彉閲廼         var i Men          //i鑳藉瓨鍌⊿tudent         i = mike銆銆銆銆         fmt.Println("This is Mike, a Student:")         i.SayHi()         i.Sing("November rain")          //i涔熻兘瀛樺偍Employee         i = tom         fmt.Println("This is tom, an Employee:")         i.SayHi()         i.Sing("Born to be wild")          //瀹氫箟浜唖lice Men         fmt.Println("Let's use a slice of Men and see what happens")         x := make([]Men, 3)         //榪欎笁涓兘鏄笉鍚岀被鍨嬬殑鍏冪礌錛屼絾鏄粬浠疄鐜頒簡interface鍚屼竴涓帴鍙?/span>         x[0], x[1], x[2] = paul, sam, mike          for _, value := range x{             value.SayHi()         }     }
澶嶅埗浠g爜

 

絀烘帴鍙?/h3>

絀篿nterface(interface{})涓嶅寘鍚換浣曠殑method錛屾鍥犱負濡傛錛?span style="background-color: #ffff00;">鎵鏈夌殑綾誨瀷閮藉疄鐜頒簡絀篿nterface銆傜┖interface瀵逛簬鎻忚堪璧蜂笉鍒頒換浣曠殑浣滅敤(鍥犱負瀹冧笉鍖呭惈浠諱綍鐨刴ethod錛夛紝浣嗘槸絀篿nterface鍦ㄦ垜浠渶瑕佸瓨鍌ㄤ換鎰忕被鍨嬬殑鏁板肩殑鏃跺欑浉褰撴湁鐢紝鍥犱負瀹冨彲浠ュ瓨鍌ㄤ換鎰忕被鍨嬬殑鏁板箋傚畠鏈夌偣綾諱技浜嶤璇█鐨剉oid*綾誨瀷銆?/span>

澶嶅埗浠g爜
// 瀹氫箟a涓虹┖鎺ュ彛     var a interface{}     var i int = 5     s := "Hello world"     // a鍙互瀛樺偍浠繪剰綾誨瀷鐨勬暟鍊?/span>     a = i     a = s
澶嶅埗浠g爜

 

interface鐨勫彉閲忛噷闈㈠彲浠ュ瓨鍌ㄤ換鎰忕被鍨嬬殑鏁板鹼紙璇ョ被鍨嬪疄鐜頒簡interface錛夛紝閭d箞鎴戜滑鎬庝箞鍙嶅悜鐭ラ亾榪欎釜interface鍙橀噺閲岄潰瀹為檯淇濆瓨浜嗙殑鏄摢涓被鍨嬬殑瀵硅薄鍛紵鐩墠甯哥敤鐨勬湁涓ょ鏂規硶錛歴witch嫻嬭瘯銆丆omma-ok鏂█銆?/p>

 

switch嫻嬭瘯濡備笅錛?/p>

澶嶅埗浠g爜
type Element interface{} type List [] Element  type Person struct {     name string     age int  }  //鎵撳嵃 func (p Person) String() string {     return "(name: " + p.name + " - age: "+strconv.Itoa(p.age)+ " years)" }  func main() {     list := make(List, 3)     list[0] = 1 //an int      list[1] = "Hello" //a string     list[2] = Person{"Dennis", 70}       for index, element := range list{         switch value := element.(type) {             case int:                 fmt.Printf("list[%d] is an int and its value is %d\n", index, value)             case string:                 fmt.Printf("list[%d] is a string and its value is %s\n", index, value)             case Person:                 fmt.Printf("list[%d] is a Person and its value is %s\n", index, value)             default:                 fmt.Println("list[%d] is of a different type", index)         }        }    }
澶嶅埗浠g爜

 

濡傛灉浣跨敤Comma-ok鏂█鐨勮瘽錛?/p>

澶嶅埗浠g爜
func main() {     list := make(List, 3)     list[0] = 1 // an int     list[1] = "Hello" // a string     list[2] = Person{"Dennis", 70}      for index, element := range list {         if value, ok := element.(int); ok {             fmt.Printf("list[%d] is an int and its value is %d\n", index, value)         } else if value, ok := element.(string); ok {             fmt.Printf("list[%d] is a string and its value is %s\n", index, value)         } else if value, ok := element.(Person); ok {             fmt.Printf("list[%d] is a Person and its value is %s\n", index, value)         } else {             fmt.Printf("list[%d] is of a different type\n", index)         }     } }
澶嶅埗浠g爜

 

 

宓屽叆鎺ュ彛

姝estruct綾誨瀷鍙互鍖呭惈涓涓尶鍚嶅瓧孌碉紝interface涔熷彲浠ュ祵濂楀彟澶栦竴涓帴鍙c?/p>

濡傛灉涓涓猧nterface1浣滀負interface2鐨勪竴涓祵鍏ュ瓧孌碉紝閭d箞interface2闅愬紡鐨勫寘鍚簡interface1閲岄潰鐨刴ethod銆?/p>

 

 

鍙嶅皠

鎵璋撳弽灝勶紙reflect錛夊氨鏄兘媯鏌ョ▼搴忓湪榪愯鏃剁殑鐘舵併?/p>

浣跨敤reflect涓鑸垎鎴愪笁姝ワ紝涓嬮潰綆瑕佺殑璁茶В涓涓嬶細瑕佸幓鍙嶅皠鏄竴涓被鍨嬬殑鍊?榪欎簺鍊奸兘瀹炵幇浜嗙┖interface)錛岄鍏堥渶瑕佹妸瀹冭漿鍖栨垚reflect瀵硅薄(reflect.Type鎴栬卹eflect.Value錛屾牴鎹笉鍚岀殑鎯呭喌璋冪敤涓嶅悓鐨勫嚱鏁?銆傝繖涓ょ鑾峰彇鏂瑰紡濡備笅錛?/p>

 t := reflect.TypeOf(i)    //寰楀埌綾誨瀷鐨勫厓鏁版嵁,閫氳繃t鎴戜滑鑳借幏鍙栫被鍨嬪畾涔夐噷闈㈢殑鎵鏈夊厓绱?/span>  v := reflect.ValueOf(i)   //寰楀埌瀹為檯鐨勫鹼紝閫氳繃v鎴戜滑鑾峰彇瀛樺偍鍦ㄩ噷闈㈢殑鍊鹼紝榪樺彲浠ュ幓鏀瑰彉鍊?/span>

 

杞寲涓簉eflect瀵硅薄涔嬪悗鎴戜滑灝卞彲浠ヨ繘琛屼竴浜涙搷浣滀簡錛屼篃灝辨槸灝唕eflect瀵硅薄杞寲鎴愮浉搴旂殑鍊鹼紝渚嬪

tag := t.Elem().Field(0).Tag  //鑾峰彇瀹氫箟鍦╯truct閲岄潰鐨勬爣絳?/span> name := v.Elem().Field(0).String()  //鑾峰彇瀛樺偍鍦ㄧ涓涓瓧孌甸噷闈㈢殑鍊?/span>

 

鑾峰彇鍙嶅皠鍊艱兘榪斿洖鐩稿簲鐨勭被鍨嬪拰鏁板?/p>

var x float64 = 3.4 v := reflect.ValueOf(x) fmt.Println("type:", v.Type()) fmt.Println("kind is float64:", v.Kind() == reflect.Float64) fmt.Println("value:", v.Float())

 

鏈鍚庯紝鍙嶅皠鐨勮瘽錛岄偅涔堝弽灝勭殑瀛楁蹇呴』鏄彲淇敼鐨勶紝鎴戜滑鍓嶉潰瀛︿範榪囦紶鍊煎拰浼犲紩鐢紝榪欎釜閲岄潰涔熸槸涓鏍風殑閬撶悊銆傚弽灝勭殑瀛楁蹇呴』鏄彲璇誨啓鐨勬剰鎬濇槸錛屽鏋滀笅闈㈣繖鏍峰啓錛岄偅涔堜細鍙戠敓閿欒

var x float64 = 3.4 v := reflect.ValueOf(x) v.SetFloat(7.1)

 

濡傛灉瑕佷慨鏀圭浉搴旂殑鍊鹼紝蹇呴』榪欐牱鍐?/p>

var x float64 = 3.4 p := reflect.ValueOf(&x) v := p.Elem() v.SetFloat(7.1)

涓婇潰鍙槸瀵瑰弽灝勭殑綆鍗曚粙緇嶏紝鏇存繁鍏ョ殑鐞嗚В榪橀渶瑕佽嚜宸卞湪緙栫▼涓笉鏂殑瀹炶返銆?/p>

 

 

鍙傝冩枃妗o細

http://se77en.cc/2014/05/05/methods-interfaces-and-embedded-types-in-golang/

http://se77en.cc/2014/05/04/choose-whether-to-use-a-value-or-pointer-receiver-on-methods/

 http://www.cnblogs.com/chenny7/p/4497969.html







abin 2017-08-03 11:34 鍙戣〃璇勮
]]>
鑰佽櫈瑕佸GoLang-鍑芥暟(涓?http://m.tkk7.com/stevenjohn/archive/2017/08/02/432718.htmlabinabinWed, 02 Aug 2017 08:39:00 GMThttp://m.tkk7.com/stevenjohn/archive/2017/08/02/432718.htmlhttp://m.tkk7.com/stevenjohn/comments/432718.htmlhttp://m.tkk7.com/stevenjohn/archive/2017/08/02/432718.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/432718.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/432718.html涓嶅彲鎴栫己鐨勫嚱鏁幫紝鍦℅o涓畾涔夊嚱鏁扮殑鏂瑰紡濡備笅錛?/p>
func (p myType ) funcName ( a, b int , c string ) ( r , s int ) {     return } 

閫氳繃鍑芥暟瀹氫箟錛屾垜浠彲浠ョ湅鍒癎o涓嚱鏁板拰鍏朵粬璇█涓殑鍏辨у拰鐗規?/p>

鍏辨?/h3>
  • 鍏抽敭瀛?#8212;—func
  • 鏂規硶鍚?#8212;—funcName
  • 鍏ュ弬——— a,b int,b string
  • 榪斿洖鍊?#8212;— r,s int
  • 鍑芥暟浣?#8212;— {}

鐗規?/h3>

Go涓嚱鏁扮殑鐗規ф槸闈炲父閰風殑錛岀粰鎴戜滑甯︽潵涓嶄竴鏍風殑緙栫▼浣撻獙銆?/p>

涓虹壒瀹氱被鍨嬪畾涔夊嚱鏁幫紝鍗充負綾誨瀷瀵硅薄瀹氫箟鏂規硶

鍦℅o涓氳繃緇欏嚱鏁版爣鏄庢墍灞炵被鍨嬶紝鏉ョ粰璇ョ被鍨嬪畾涔夋柟娉曪紝涓婇潰鐨?nbsp;p myType 鍗寵〃紺虹粰myType澹版槑浜嗕竴涓柟娉曪紝 p myType 涓嶆槸蹇呴』鐨勩傚鏋滄病鏈夛紝鍒欑函綺規槸涓涓嚱鏁幫紝閫氳繃鍖呭悕縐拌闂俻ackageName.funcationName

濡傦細

//瀹氫箟鏂扮殑綾誨瀷double錛屼富瑕佺洰鐨勬槸緇檉loat64綾誨瀷鎵╁厖鏂規硶 type double float64  //鍒ゆ柇a鏄惁絳変簬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 }  //鍒ゆ柇a鏄惁絳変簬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) ) )  } 

涓婅堪紺轟緥涓?float64 鍩烘湰綾誨瀷鎵╁厖浜嗘柟娉旾sEqual錛岃鏂規硶涓昏鏄В鍐崇簿搴﹂棶棰樸?鍏舵柟娉曡皟鐢ㄦ柟寮忎負錛?nbsp;a.IsEqual(double) 錛屽鏋滀笉鎵╁厖鏂規硶錛屾垜浠彧鑳戒嬌鐢ㄥ嚱鏁?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)

鍏ュ弬涓紝濡傛灉榪炵畫鐨勫弬鏁扮被鍨嬩竴鑷達紝鍒欏彲浠ョ渷鐣ヨ繛緇涓弬鏁扮殑綾誨瀷錛屽彧淇濈暀鏈鍚庝竴涓被鍨嬪0鏄庛?/h4>

濡?nbsp;func IsEqual(a, b float64) bool 榪欎釜鏂規硶灝卞彧淇濈暀浜嗕竴涓被鍨嬪0鏄?姝ゆ椂鍏ュ弬a鍜宐鍧囨槸float64鏁版嵁綾誨瀷銆?榪欐牱涔熸槸鍙互鐨勶細 func IsEqual(a, b float64, accuracy int) bool

鍙樺弬錛氬叆鍙傛敮鎸佸彉鍙?鍗沖彲鎺ュ彈涓嶇‘瀹氭暟閲忕殑鍚屼竴綾誨瀷鐨勫弬鏁?/h4>

濡?nbsp;func Sum(args ...int) 鍙傛暟args鏄殑slice錛屽叾鍏冪礌綾誨瀷涓篿nt 銆傜粡甯鎬嬌鐢ㄧ殑fmt.Printf灝辨槸涓涓帴鍙椾換鎰忎釜鏁板弬鏁扮殑鍑芥暟 fmt.Printf(format string, args ...interface{})

鏀寔澶氳繑鍥炲?/h4>

鍓嶉潰鎴戜滑瀹氫箟鍑芥暟鏃惰繑鍥炲兼湁涓や釜r,s 銆傝繖鏄潪甯告湁鐢ㄧ殑錛屾垜鍦ㄥ啓C#浠g爜鏃訛紝甯稿父涓轟簡浠庡凡鏈夊嚱鏁頒腑鑾峰緱鏇村鐨勪俊鎭紝闇瑕佷慨鏀瑰嚱鏁扮鍚嶏紝浣跨敤out ,ref 絳夋柟寮忓幓鑾峰緱鏇村榪斿洖緇撴灉銆傝岀幇鍦ㄤ嬌鐢℅o鏃跺垯寰堢畝鍗曪紝鐩存帴鍦ㄨ繑鍥炲煎悗闈㈡坊鍔犺繑鍥炲弬鏁板嵆鍙?/p>

濡?鍦–#涓竴涓瓧絎︿覆杞崲涓篿nt綾誨瀷鏃墮昏緫浠g爜

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

鑰屽湪Go涓紝鍒欏彲浠ヨ繖鏍峰疄鐜?閫昏緫綺劇畝鑰屾槑紜?/p>

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

鍚屾椂鍦℅o涓緢澶氬嚱鏁板厖鍒嗗埄鐢ㄤ簡澶氳繑鍥炲?/p>

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

閭d箞濡傛灉鎴戝彧闇瑕佹煇涓涓繑鍥炲鹼紝鑰屼笉鍏沖績鍏朵粬榪斿洖鍊肩殑璇濓紝鎴戣濡備綍鍔炲憿錛?榪欐椂鍙互綆鍗曠殑浣跨敤絎﹀彿涓嬪垝綰?#8221;_“ 鏉ュ拷鐣ヤ笉鍏沖績鐨勮繑鍥炲箋傚錛?/p>

_, cos = math.Sincos(3.1415) //鍙渶瑕乧os璁$畻鐨勫?

鍛藉悕榪斿洖鍊?/h4>

鍓嶉潰鎴戜滑璇翠簡鍑芥暟鍙互鏈夊涓繑鍥炲鹼紝榪欓噷鎴戣繕瑕佽鐨勬槸錛屽湪鍑芥暟瀹氫箟鏃跺彲浠ョ粰鎵鏈夌殑榪斿洖鍊煎垎鍒懡鍚嶏紝榪欐牱灝辮兘鍦ㄥ嚱鏁頒腑浠繪剰浣嶇疆緇欎笉鍚岃繑鍥炲煎鍒訛紝鑰屼笉闇瑕佸湪return璇彞涓墠鎸囧畾榪斿洖鍊箋傚悓鏃朵篃鑳藉寮哄彲璇繪э紝涔熸彁楂榞odoc鎵鐢熸垚鏂囨。鐨勫彲璇繪?/p>

濡傛灉涓嶆敮鎸佸懡鍚嶈繑鍥炲鹼紝鎴戝彲鑳戒細鏄繖鏍峰仛鐨?/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 } 

浣嗘敮鎸佺粰榪斿洖鍊煎懡鍚嶅悗錛屽疄闄呬笂灝辨槸鐪佺暐浜嗗彉閲忕殑澹版槑錛宺eturn鏃舵棤闇鍐欐垚return n,err 鑰屾槸灝嗙洿鎺ュ皢鍊艱繑鍥?/p>

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 } 

鍑芥暟涔熸槸“鍊?#8221;

鍜孏o涓叾浠栦笢瑗夸竴鏍鳳紝鍑芥暟涔熸槸鍊鹼紝榪欐牱灝卞彲浠ュ0鏄庝竴涓嚱鏁扮被鍨嬬殑鍙橀噺錛屽皢鍑芥暟浣滀負鍙傛暟浼犻掋?/p>

澹版槑鍑芥暟涓哄肩殑鍙橀噺(鍖垮悕鍑芥暟:鍙祴鍊間釜鍙橀噺錛屼篃鍙洿鎺ユ墽琛?

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

杈撳嚭緇撴灉濡備笅錛岃繖閲岃〃鏄巉c 鐨勭被鍨嬩負錛歠unc(string)

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

灝嗗嚱鏁頒綔涓哄叆鍙傦紙鍥炶皟鍑芥暟錛夛紝鑳藉甫鏉ヤ究鍒┿傚鏃ュ織澶勭悊錛屼負浜嗙粺涓澶勭悊錛屽皢淇℃伅鍧囬氳繃鎸囧畾鍑芥暟鍘昏褰曟棩蹇楋紝涓旀槸鍚﹁褰曟棩蹇楄繕鏈夊紑鍏?/p>

func Log(title string, getMsg func() string) {     //濡傛灉寮鍚棩蹇楄褰?鍒欒褰曟棩蹇?    if true {         fmt.Println(title, ":", getMsg())     } } //---------璋冪敤-------------- count := 0 msg := func() string {     count++     return "鎮ㄦ病鏈夊嵆浣挎彁閱掓垜,宸茶Е鐘硶寰? } Log("error", msg) Log("warring", msg) Log("info", msg) fmt.Println(count) 

榪欓噷杈撳嚭緇撴灉濡備笅錛宑ount 涔熷彂鐢熶簡鍙樺寲

error : 鎮ㄦ病鏈夊嵆浣挎彁閱掓垜,宸茶Е鐘硶寰?warring : 鎮ㄦ病鏈夊嵆浣挎彁閱掓垜,宸茶Е鐘硶寰?info : 鎮ㄦ病鏈夊嵆浣挎彁閱掓垜,宸茶Е鐘硶寰?3 

鍑芥暟涔熸槸“綾誨瀷”

浣犳湁娌℃湁娉ㄦ剰鍒頒笂闈㈢ず渚嬩腑鐨?nbsp;fc := func(msg string)... 錛屾棦鐒跺尶鍚嶅嚱鏁板彲浠ヨ祴鍊肩粰涓涓彉閲忥紝鍚屾椂鎴戜滑緇忓父榪欐牱緇檌nt璧嬪?nbsp;value := 2 ,鏄惁鎴戜滑鍙互澹版槑func(string) 綾誨瀷 鍛紝褰撶劧鏄彲浠ョ殑銆?/p>

//涓涓褰曟棩蹇楃殑綾誨瀷錛歠unc(string) type saveLog func(msg string)  //灝嗗瓧絎︿覆杞崲涓篿nt64,濡傛灉杞崲澶辮觸璋冪敤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     } }  //璁板綍鏃ュ織娑堟伅鐨勫叿浣撳疄鐜?func myLog(msg string) {     fmt.Println("Find Error:", msg) }  func main() {     stringToInt("123", myLog) //杞崲鏃跺皢璋冪敤mylog璁板綍鏃ュ織     stringToInt("s", myLog) } 

榪欓噷鎴戜滑瀹氫箟浜嗕竴涓被鍨嬶紝涓撻棬鐢ㄤ綔璁板綍鏃ュ織鐨勬爣鍑嗘帴鍙c傚湪stringToInt鍑芥暟涓鏋滆漿鎹㈠け璐ュ垯璋冪敤鎴戣嚜宸卞畾涔夌殑鎺ュ彛鍑芥暟榪涜鏃ュ織澶勭悊錛岃嚦浜庢渶緇堟墽琛岀殑鍝釜鍑芥暟錛屽垯鏃犻渶鍏沖績銆?/p>

defer 寤惰繜鍑芥暟

defer 鍙堟槸涓涓垱鏂幫紝瀹冪殑浣滅敤鏄細寤惰繜鎵ц錛屽湪澹版槑鏃朵笉浼氱珛鍗蟲墽琛岋紝鑰屾槸鍦ㄥ嚱鏁皉eturn鍚庢椂鎸夌収鍚庤繘鍏堝嚭鐨勫師鍒欎緷嬈℃墽琛屾瘡涓涓猟efer銆傝繖鏍峰甫鏉ョ殑濂藉鏄紝鑳界‘淇濇垜浠畾涔夌殑鍑芥暟鑳界櫨鍒嗕箣鐧捐兘澶熻鎵ц鍒幫紝榪欐牱灝辮兘鍋氬緢澶氭垜浠兂鍋氱殑浜嬶紝濡傞噴鏀捐祫婧愶紝娓呯悊鏁版嵁錛岃褰曟棩蹇楃瓑

榪欓噷鎴戜滑閲嶇偣鏉ヨ鏄庝笅defer鐨勬墽琛岄『搴?/p>

func deferFunc() int {     index := 0      fc := func() {          fmt.Println(index, "鍖垮悕鍑芥暟1")         index++          defer func() {             fmt.Println(index, "鍖垮悕鍑芥暟1-1")             index++         }()     }      defer func() {         fmt.Println(index, "鍖垮悕鍑芥暟2")         index++     }()      defer fc()      return func() int {         fmt.Println(index, "鍖垮悕鍑芥暟3")         index++         return index     }() }  func main() {     deferFunc() } 

榪欓噷杈撳嚭緇撴灉濡備笅錛?/p>

0 鍖垮悕鍑芥暟3 1 鍖垮悕鍑芥暟1 2 鍖垮悕鍑芥暟1-1 3 鍖垮悕鍑芥暟2 

鏈夊涓嬬粨璁猴細

  • defer 鏄湪鎵ц瀹宺eturn 鍚庢墽琛?/li>
  • defer 鍚庤繘鍏堟墽琛?/li>

鍙﹀錛屾垜浠父浣跨敤defer鍘誨叧闂璉O,鍦ㄦ甯告墦寮鏂囦歡鍚庯紝灝辯珛鍒誨0鏄庝竴涓猟efer錛岃繖鏍峰氨涓嶄細蹇樿鍏抽棴鏂囦歡錛屼篃鑳戒繚璇佸湪鍑虹幇寮傚父絳変笉鍙鏂欑殑鎯呭喌涓嬩篃鑳藉叧闂枃浠躲傝屼笉鍍忓叾浠栬璦錛?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 鎴栬?nbsp;using() 鏂瑰紡榪涜澶勭悊銆?/p>

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

鍚庣畫錛屾垜灝嗚璁猴細 浣滅敤鍩熴佷紶鍊煎拰浼犳寚閽?浠ュ強 淇濈暀鍑芥暟init(),main()

鏈瑪璁頒腑鎵鍐欎唬鐮佸瓨鍌ㄤ綅緗細



abin 2017-08-02 16:39 鍙戣〃璇勮
]]>
MySQL瑙﹀彂鍣═rigger瀹炰緥綃?http://m.tkk7.com/stevenjohn/archive/2016/08/18/431625.htmlabinabinThu, 18 Aug 2016 09:25:00 GMThttp://m.tkk7.com/stevenjohn/archive/2016/08/18/431625.htmlhttp://m.tkk7.com/stevenjohn/comments/431625.htmlhttp://m.tkk7.com/stevenjohn/archive/2016/08/18/431625.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/431625.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/431625.html
MySQL瑙﹀彂鍣═rigger瀹炰緥綃?/div>
鍙戣〃浜?68 澶╁墠 ⁄ IT鎶鏈?/a> ⁄ 鏆傛棤璇勮

浠ュ墠鍏蟲敞鐨勬暟鎹瓨鍌ㄨ繃紼嬩笉澶噦鍏朵腑濂ュ錛屾渶榪戦亣鍒拌法鏁版嵁搴?/a>錛屽悓鏃跺澶氫釜琛ㄨ繘琛孋URD錛圕reate澧炪乁pdate鏀廣丷ead璇匯丏elete鍒狅級錛屾庝箞鎵嶈兘璁╃箒鐞愮殑鏁版嵁CURD鍚屾鍙樺緱鏇村鏄撳憿錛熺浉淇″緢澶氫漢浼氶鍏堟兂鍒頒簡MySQL瀛樺偍榪囩▼銆佽Е鍙戝櫒錛岃繖縐嶆兂娉曠‘瀹炰笉閿欍備簬鏄ザ鏈夊叴瓚e湴浜茶嚜鍐欎簡CUD錛堝銆佹敼銆佸垹錛夎Е鍙戝櫒鐨勫疄渚嬶紝鐢ㄨЕ鍙戝櫒瀹炵幇澶氳〃鏁版嵁鍚屾鏇存柊銆?/p>

MySQL瑙﹀彂鍣═rigger瀹炰緥綃? title=

瀹氫箟錛?浣曚負MySQL瑙﹀彂鍣紵

鍦∕ySQL Server閲岄潰涔熷氨鏄鏌愪竴涓〃鐨勪竴瀹氱殑鎿嶄綔錛岃Е鍙戞煇縐嶆潯浠訛紙Insert,Update,Delete 絳夛級錛屼粠鑰岃嚜鍔ㄦ墽琛岀殑涓孌電▼搴忋備粠榪欑鎰忎箟涓婅瑙﹀彂鍣ㄦ槸涓涓壒孌婄殑瀛樺偍榪囩▼銆備笅闈㈤氳繃MySQL瑙﹀彂鍣ㄥ疄渚嬶紝鏉ヤ簡瑙d竴涓嬭Е鍙戝櫒鐨勫伐浣滆繃紼嬪惂錛?/p>

涓銆佸垱寤篗ySQL瀹炰緥鏁版嵁琛細

鍦╩ysql鐨勯粯璁ょ殑嫻嬭瘯test鏁版嵁搴撲笅錛屽垱寤轟袱涓〃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;

鍦╰_a琛ㄤ笂鍒嗗垱寤轟竴涓狢UD錛堝銆佹敼銆佸垹錛?涓Е鍙戝櫒錛屽皢t_a鐨勮〃鏁版嵁涓巘_b鍚屾瀹炵幇CUD錛屾敞鎰忓垱寤鴻Е鍙戝櫒姣忎釜琛ㄥ悓綾諱簨浠舵湁涓斾粎鏈変竴涓搴旇Е鍙戝櫒錛屼負浠涔堝彧鑳藉涓涓Е鍙戝櫒錛屼笉瑙i噴鍟︼紝鐪婱YSQL鐨勮鏄庡府鍔╂枃妗e惂銆?/p>

浜屻佸垱寤篗ySQL瀹炰緥瑙﹀彂鍣細

鍦ㄥ疄渚嬫暟鎹〃t_a涓婁緷嬈℃寜鐓т笅闈㈡楠ゅ垱寤簍r_a_insert銆乼r_a_update銆乼r_a_delete涓変釜瑙﹀彂鍣?/p>

1銆佸垱寤篒NSERT瑙﹀彂鍣╰rigger_a_insert錛?/strong>



    DELIMITER $$
     
    USE `test`$$
     
    --鍒ゆ柇鏁版嵁搴撲腑鏄惁瀛樺湪tr_a_insert瑙﹀彂鍣?br />    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_insert`$$
    --涓嶅瓨鍦╰r_a_insert瑙﹀彂鍣紝寮濮嬪垱寤鴻Е鍙戝櫒
    --Trigger瑙﹀彂鏉′歡涓篿nsert鎴愬姛鍚庤繘琛岃Е鍙?br />    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_insert` AFTER INSERT ON `t_a`
        FOR EACH ROW BEGIN
            --Trigger瑙﹀彂鍚庯紝鍚屾椂瀵箃_b鏂板鍚屾涓鏉℃暟鎹?br />            INSERT INTO `t_b` SET username = NEW.username, groupid=NEW.groupid;
        END;
    $$
     
    DELIMITER;
2銆佸垱寤篣PDATE瑙﹀彂鍣╰rigger_a_update錛?/strong>


    DELIMITER $$
     
    USE `test`$$
    --鍒ゆ柇鏁版嵁搴撲腑鏄惁瀛樺湪tr_a_update瑙﹀彂鍣?br />    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_update`$$
    --涓嶅瓨鍦╰r_a_update瑙﹀彂鍣紝寮濮嬪垱寤鴻Е鍙戝櫒
    --Trigger瑙﹀彂鏉′歡涓簎pdate鎴愬姛鍚庤繘琛岃Е鍙?br />    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_update` AFTER UPDATE ON `t_a`
        FOR EACH ROW BEGIN
        --Trigger瑙﹀彂鍚庯紝褰搕_a琛╣roupid,username鏁版嵁鏈夋洿鏀規椂錛屽t_b琛ㄥ悓姝ヤ竴鏉℃洿鏂板悗鐨勬暟鎹?br />          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銆佸垱寤篋ELETE瑙﹀彂鍣╰rigger_a_delete錛?/strong>


    DELIMITER $$
     
    USE `test`$$
    --鍒ゆ柇鏁版嵁搴撲腑鏄惁瀛樺湪tr_a_delete瑙﹀彂鍣?br />    DROP TRIGGER /*!50032 IF EXISTS */ `tr_a_delete`$$
    --涓嶅瓨鍦╰r_a_delete瑙﹀彂鍣紝寮濮嬪垱寤鴻Е鍙戝櫒
    --Trigger瑙﹀彂鏉′歡涓篸elete鎴愬姛鍚庤繘琛岃Е鍙?br />    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tr_a_delete` AFTER DELETE ON `t_a`
        FOR EACH ROW BEGIN
            --t_a琛ㄦ暟鎹垹闄ゅ悗錛宼_b琛ㄥ叧鑱旀潯浠剁浉鍚岀殑鏁版嵁涔熷悓姝ュ垹闄?br />            DELETE FROM `t_b` WHERE username=Old.username AND groupid=OLD.groupid;
        END;
    $$
     
    DELIMITER ;

涓夈佹祴璇昅ySQL瀹炰緥瑙﹀彂鍣細

鍒嗗埆嫻嬭瘯瀹炵幇t_a涓巘_b瀹炵幇鏁版嵁鍚屾CUD(澧炪佹敼銆佸垹)3涓猅riggers

1銆佹祴璇昅ySQL鐨勫疄渚媡r_a_insert瑙﹀彂鍣細

鍦╰_a琛ㄤ腑鏂板涓鏉℃暟鎹紝鐒跺悗鍒嗗埆鏌ヨt_a/t_b琛ㄧ殑鏁版嵁鏄惁鏁版嵁鍚屾錛屾祴璇曡Е鍙戝櫒鎴愬姛鏍囧織錛宼_a琛ㄦ棤璁哄湪浣曠鎯呭喌涓嬶紝鏂板浜嗕竴鏉℃垨澶氭潯璁板綍闆嗘椂錛屾病鏈塼_b琛ㄥ仛浠諱綍鏁版嵁insert鎿嶄綔錛屽畠鍚屾椂鏂板浜嗕竴鏍風殑澶氭潯璁板綍闆嗐?/p>

涓嬮潰鏉ヨ繘琛孧ySQL瑙﹀彂鍣ㄥ疄渚嬫祴璇曪細



    --t_a琛ㄦ柊澧炰竴鏉¤褰曢泦
        INSERT INTO `t_a` (username,groupid) VALUES ('sky54.net',123)
       
        --鏌ヨt_a琛?br />        SELECT id,username,groupid FROM `t_a`
       
        --鏌ヨt_b琛?br />        SELECT id,username,groupid FROM `t_b`

2銆佹祴璇昅ySQL鐨勫疄渚媡r_a_update銆乼r_a_delete瑙﹀彂鍣細

榪欎袱涓狹ySQL瑙﹀彂鍣ㄦ祴璇曞師鐞嗐佹楠や笌tr_a_insert瑙﹀彂鍣ㄤ竴鏍風殑錛屽厛淇敼/鍒犻櫎涓鏉℃暟鎹紝鐒跺悗鍒嗗埆鏌ョ湅t_a銆乼_b琛ㄧ殑鏁版嵁鍙樺寲鎯呭喌錛屾暟鎹彉鍖栧悓姝ヨ鏄嶵rigger瀹炰緥鎴愬姛錛屽惁鍒欓渶瑕侀愭鎺掓煡閿欒鍘熷洜銆?/p>

涓栫晫涓婁換浣曚竴縐嶄簨鐗╅兘鍏跺叾浼樼偣鍜岀己鐐癸紝浼樼偣涓庣己鐐規槸鑷韓涓涓浉瀵圭珛鐨勯潰銆傚綋鐒惰繖閲屼笉鏄己璋?#8220;涓栫晫闈為粦鍗崇櫧”寮忕殑“浜屽厓璁?#8221;錛?#8220;瀛樺湪鍗沖悎鐞?#8221;鍢涖傚綋鐒?MySQL瑙﹀彂鍣ㄧ殑浼樼偣涓嶈浜嗭紝璇翠竴涓嬩笉瓚充箣澶勶紝MySQL Trigger娌℃湁寰堝ソ鐨勮皟璇曘佺鐞嗙幆澧冿紝闅句簬鍦ㄥ悇縐嶇郴緇熺幆澧冧笅嫻嬭瘯錛屾祴璇曟瘮MySQL瀛樺偍榪囩▼瑕侀毦錛屾墍浠ュ緩璁湪鐢熸垚鐜涓嬶紝灝介噺鐢ㄥ瓨鍌ㄨ繃紼嬫潵浠f浛 MySQL瑙﹀彂鍣ㄣ?/p>

鏈瘒緇撴潫鍓嶅啀寮鴻皟涓涓嬶紝鏀寔瑙﹀彂鍣ㄧ殑MySQL鐗堟湰闇瑕?.0浠ヤ笂錛?.0浠ュ墠鐗堟湰鐨凪ySQL鍗囩駭鍒?.0浠ュ悗鐗堟湰鏂瑰彲浣跨敤瑙﹀彂鍣ㄥ摝錛?/p>








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





abin 2016-08-18 17:25 鍙戣〃璇勮
]]>
鑱婅亰楂樺茍鍙戠郴緇熶箣闄愭祦鐗規妧-1http://m.tkk7.com/stevenjohn/archive/2016/06/14/430882.htmlabinabinTue, 14 Jun 2016 05:38:00 GMThttp://m.tkk7.com/stevenjohn/archive/2016/06/14/430882.htmlhttp://m.tkk7.com/stevenjohn/comments/430882.htmlhttp://m.tkk7.com/stevenjohn/archive/2016/06/14/430882.html#Feedback1http://m.tkk7.com/stevenjohn/comments/commentRss/430882.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/430882.html闃呰鍏ㄦ枃

abin 2016-06-14 13:38 鍙戣〃璇勮
]]>
shadowsocks 瀹夎http://m.tkk7.com/stevenjohn/archive/2016/05/13/430493.htmlabinabinFri, 13 May 2016 14:56:00 GMThttp://m.tkk7.com/stevenjohn/archive/2016/05/13/430493.htmlhttp://m.tkk7.com/stevenjohn/comments/430493.htmlhttp://m.tkk7.com/stevenjohn/archive/2016/05/13/430493.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/430493.htmlhttp://m.tkk7.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 鍙戣〃璇勮
]]>
httpClient Https 鍗曞悜涓嶉獙璇?httpClient榪炴帴姹? http://m.tkk7.com/stevenjohn/archive/2016/04/27/430267.htmlabinabinWed, 27 Apr 2016 11:04:00 GMThttp://m.tkk7.com/stevenjohn/archive/2016/04/27/430267.htmlhttp://m.tkk7.com/stevenjohn/comments/430267.htmlhttp://m.tkk7.com/stevenjohn/archive/2016/04/27/430267.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/430267.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/430267.html搴熻瘽灝戣錛岀洿鎺ヤ笂浠g爜錛屼互鍓嶉兘鏄皟鐢ㄥ埆浜哄啓濂界殑錛岀幇鍦ㄦ湁鏃墮棿鑷繁寮勪笅錛屽叿浣撳姛鑳藉涓嬶細
1銆乭ttpClient+http+綰跨▼姹狅細
2銆乭ttpClient+https(鍗曞悜涓嶉獙璇佽瘉涔?+綰跨▼姹狅細

https鍦?TOMCAT_HOME%/conf/server.xml閲岄潰鐨勯厤緗枃浠?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"/> 
鍏朵腑 clientAuth="false"琛ㄧず涓嶅紑鍚瘉涔﹂獙璇侊紝鍙槸鍗曞瓨鐨勮蛋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: 涓婂崍10: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);
//棣栧厛璁劇疆鍏ㄥ眬鐨勬爣鍑哻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;
}

// 閰嶇疆璇鋒眰鐨勮秴鏃惰緗?br /> //棣栧厛璁劇疆鍏ㄥ眬鐨勬爣鍑哻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(){
//璇鋒眰閲嶈瘯澶勭悊
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception,int executionCount, HttpContext context) {
if (executionCount >= 5) {// 濡傛灉宸茬粡閲嶈瘯浜?嬈★紝灝辨斁寮?br /> return false;
}
if (exception instanceof NoHttpResponseException) {// 濡傛灉鏈嶅姟鍣ㄤ涪鎺変簡榪炴帴錛岄偅涔堝氨閲嶈瘯
return true;
}
if (exception instanceof SSLHandshakeException) {// 涓嶈閲嶈瘯SSL鎻℃墜寮傚父
return false;
}
if (exception instanceof InterruptedIOException) {// 瓚呮椂
return false;
}
if (exception instanceof UnknownHostException) {// 鐩爣鏈嶅姟鍣ㄤ笉鍙揪
return false;
}
if (exception instanceof ConnectTimeoutException) {// 榪炴帴琚嫆緇?br /> return false;
}
if (exception instanceof SSLException) {// ssl鎻℃墜寮傚父
return false;
}

HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 濡傛灉璇鋒眰鏄箓絳夌殑錛屽氨鍐嶆灝濊瘯
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
return httpRequestRetryHandler;
}



//鍒涘緩HostnameVerifier
//鐢ㄤ簬瑙e喅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("璇鋒眰鍙傛暟涓嶈兘涓虹┖");
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("璇鋒眰鍙傛暟涓嶈兘涓虹┖");
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 鍙戣〃璇勮
]]>
Redis 浠g悊鏈嶅姟Twemproxy http://m.tkk7.com/stevenjohn/archive/2015/11/03/428041.htmlabinabinTue, 03 Nov 2015 11:30:00 GMThttp://m.tkk7.com/stevenjohn/archive/2015/11/03/428041.htmlhttp://m.tkk7.com/stevenjohn/comments/428041.htmlhttp://m.tkk7.com/stevenjohn/archive/2015/11/03/428041.html#Feedback0http://m.tkk7.com/stevenjohn/comments/commentRss/428041.htmlhttp://m.tkk7.com/stevenjohn/services/trackbacks/428041.html

1銆乼wemproxy explore

      褰撴垜浠湁澶ч噺 Redis 鎴?Memcached 鐨勬椂鍊欙紝閫氬父鍙兘閫氳繃瀹㈡埛绔殑涓浜涙暟鎹垎閰嶇畻娉曪紙姣斿涓鑷存у搱甯岋級錛?/span>鏉ュ疄鐜伴泦緹ゅ瓨鍌ㄧ殑鐗規с?/span>铏界劧Redis 2.6鐗堟湰宸茬粡鍙戝竷Redis Cluster錛屼絾榪樹笉鏄緢鎴愮啛閫傜敤姝e紡鐢熶駭鐜銆?span style="color:#333333; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"> Redis 鐨?Cluster 鏂規榪樻病鏈夋寮忔帹鍑轟箣鍓嶏紝鎴戜滑閫氳繃 Proxy 鐨勬柟寮忔潵瀹炵幇闆嗙兢瀛樺偍銆?/span>

       Twitter錛屼笘鐣屾渶澶х殑Redis闆嗙兢涔嬩竴閮ㄧ講鍦═witter鐢ㄤ簬涓虹敤鎴鋒彁渚涙椂闂磋醬鏁版嵁銆俆witter Open Source閮ㄩ棬鎻愪緵浜員wemproxy銆?/span>

     Twemproxy,涔熷彨nutcraker銆傛槸涓涓猼wtter寮婧愮殑涓涓猺edis鍜宮emcache浠g悊鏈嶅姟鍣ㄣ?redis浣滀負涓涓珮鏁堢殑緙撳瓨鏈嶅姟鍣紝闈炲父鍏鋒湁搴旂敤浠峰箋備絾鏄綋浣跨敤姣旇緝澶氱殑鏃跺欙紝灝卞笇鏈涘彲浠ラ氳繃鏌愮鏂瑰紡 緇熶竴榪?/span>琛岀鐞嗐傞伩鍏嶆瘡涓簲鐢ㄦ瘡涓鎴風綆$悊榪炴帴鐨勬澗鏁fс傚悓鏃跺湪涓瀹氱▼搴︿笂鍙樺緱鍙互鎺у埗銆?/p>

      Twemproxy鏄竴涓揩閫熺殑鍗曠嚎紼嬩唬鐞嗙▼搴忥紝鏀寔Memcached ASCII鍗忚鍜屾洿鏂扮殑Redis鍗忚錛?/span>

     瀹冨叏閮ㄧ敤C鍐欐垚錛屼嬌鐢ˋpache 2.0 License鎺堟潈銆傞」鐩湪Linux涓婂彲浠ュ伐浣滐紝鑰屽湪OSX涓婃棤娉曠紪璇戯紝鍥犱負瀹冧緷璧栦簡epoll API.

      Twemproxy 閫氳繃寮曞叆涓涓唬鐞嗗眰錛屽彲浠ュ皢鍏跺悗绔殑澶氬彴 Redis 鎴?Memcached 瀹炰緥榪涜緇熶竴綆$悊涓庡垎閰嶏紝浣垮簲鐢ㄧ▼搴忓彧闇瑕佸湪 Twemproxy 涓婅繘琛屾搷浣滐紝鑰屼笉鐢ㄥ叧蹇冨悗闈㈠叿浣撴湁澶氬皯涓湡瀹炵殑 Redis 鎴?Memcached 瀛樺偍銆?/span> 

2銆?/span>twemproxy鐗規э細

    • 鏀寔澶辮觸鑺傜偣鑷姩鍒犻櫎

      • 鍙互璁劇疆閲嶆柊榪炴帴璇ヨ妭鐐圭殑鏃墮棿
      • 鍙互璁劇疆榪炴帴澶氬皯嬈′箣鍚庡垹闄よ鑺傜偣
      • 璇ユ柟寮忛傚悎浣滀負cache瀛樺偍
    • 鏀寔璁劇疆HashTag

      • 閫氳繃HashTag鍙互鑷繁璁懼畾灝嗕袱涓狵EYhash鍒板悓涓涓疄渚嬩笂鍘匯?/li>
    • 鍑忓皯涓巖edis鐨勭洿鎺ヨ繛鎺ユ暟

      • 淇濇寔涓巖edis鐨勯暱榪炴帴
      • 鍙緗唬鐞嗕笌鍚庡彴姣忎釜redis榪炴帴鐨勬暟鐩?/li>
    • 鑷姩鍒嗙墖鍒板悗绔涓猺edis瀹炰緥涓?/p>

      • 澶氱hash綆楁硶錛?span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25px">鑳藉浣跨敤涓嶅悓鐨勭瓥鐣ュ拰鏁e垪鍑芥暟鏀寔涓鑷存ash銆?/span>
      • 鍙互璁劇疆鍚庣瀹炰緥鐨勬潈閲?/li>
    • 閬垮厤鍗曠偣闂

      • 鍙互騫寵閮ㄧ講澶氫釜浠g悊灞?client鑷姩閫夋嫨鍙敤鐨勪竴涓?/li>
    • 鏀寔redis pipelining request

           鏀寔璇鋒眰鐨勬祦寮忎笌鎵瑰鐞嗭紝闄嶄綆鏉ュ洖鐨勬秷鑰?/span>

    • 鏀寔鐘舵佺洃鎺?/p>

      • 鍙緗姸鎬佺洃鎺p鍜岀鍙o紝璁塊棶ip鍜岀鍙e彲浠ュ緱鍒頒竴涓猨son鏍煎紡鐨勭姸鎬佷俊鎭覆
      • 鍙緗洃鎺т俊鎭埛鏂伴棿闅旀椂闂?/li>
    • 楂樺悶鍚愰噺

      • 榪炴帴澶嶇敤錛屽唴瀛樺鐢ㄣ?/li>
      • 灝嗗涓繛鎺ヨ姹傦紝緇勬垚reids pipelining緇熶竴鍚憆edis璇鋒眰銆?/li>

     鍙﹀鍙互淇敼redis鐨勬簮浠g爜錛屾娊鍙栧嚭redis涓殑鍓嶅崐閮ㄥ垎錛屼綔涓轟竴涓腑闂翠唬鐞嗗眰銆傛渶緇堥兘鏄氳繃linux涓嬬殑epoll 浜嬩歡鏈哄埗鎻愰珮騫跺彂鏁堢巼錛屽叾涓璶utcraker鏈韓涔熸槸浣跨敤epoll鐨勪簨浠舵満鍒躲傚茍涓斿湪鎬ц兘嫻嬭瘯涓婄殑琛ㄧ幇闈炲父鍑鴻壊銆?/p>

3銆乼wemproxy闂涓庝笉瓚?/span>


Twemproxy 鐢變簬鍏惰嚜韜師鐞嗛檺鍒訛紝鏈変竴浜涗笉瓚充箣澶勶紝濡傦細 
  • 涓嶆敮鎸侀拡瀵瑰涓肩殑鎿嶄綔錛屾瘮濡傚彇sets鐨勫瓙浜ゅ茍琛ョ瓑錛圡GET 鍜?DEL 闄ゅ錛?/span>
  • 涓嶆敮鎸丷edis鐨勪簨鍔℃搷浣?/span>
  • 鍑洪敊鎻愮ず榪樹笉澶熷畬鍠?/span>
  • 涔熶笉鏀寔select鎿嶄綔

4銆佸畨瑁呬笌閰嶇疆 

鍏蜂綋鐨勫畨瑁呮楠ゅ彲鐢ㄦ煡鐪媑ithub錛?a target="_blank">https://github.com/twitter/twemproxy
Twemproxy 鐨勫畨瑁咃紝涓昏鍛戒護濡備笅錛?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

閫氳繃涓婇潰鐨勫懡浠ゅ氨綆楀畨瑁呭ソ浜嗭紝鐒跺悗鏄叿浣撶殑閰嶇疆錛屼笅闈㈡槸涓涓吀鍨嬬殑閰嶇疆 
    redis1:  
      listen: 127.0.0.1:6379 #浣跨敤鍝釜绔彛鍚姩Twemproxy  
      redis: true #鏄惁鏄疪edis鐨刾roxy  
      hash: fnv1a_64 #鎸囧畾鍏蜂綋鐨刪ash鍑芥暟  
      distribution: ketama #鍏蜂綋鐨刪ash綆楁硶  
      auto_eject_hosts: true #鏄惁鍦ㄧ粨鐐規棤娉曞搷搴旂殑鏃跺欎復鏃舵憳闄ょ粨鐐? 
      timeout: 400 #瓚呮椂鏃墮棿錛堟縐掞級  
      server_retry_timeout: 2000 #閲嶈瘯鐨勬椂闂達紙姣錛? 
      server_failure_limit: 1 #緇撶偣鏁呴殰澶氬皯嬈″氨綆楁憳闄ゆ帀  
      servers: #涓嬮潰琛ㄧず鎵鏈夌殑Redis鑺傜偣錛圛P:绔彛鍙?鏉冮噸錛? 
       - 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 

浣犲彲浠ュ悓鏃跺紑鍚涓?Twemproxy 瀹炰緥錛屽畠浠兘鍙互榪涜璇誨啓錛岃繖鏍蜂綘鐨勫簲鐢ㄧ▼搴忓氨鍙互瀹屽叏閬垮厤鎵璋撶殑鍗曠偣鏁呴殰銆?


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


abin 2015-11-03 19:30 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 国产精品亚洲综合| 亚洲av片不卡无码久久| 国产精品亚洲va在线观看| 19禁啪啪无遮挡免费网站| 久久精品国产亚洲一区二区| 污网站免费在线观看| 亚洲 国产 图片| 一级毛片免费在线播放| 又黄又爽的视频免费看| 黄网站色视频免费看无下截| 免费国产高清视频| japanese色国产在线看免费| 国产亚洲成人久久| 麻豆精品不卡国产免费看| 久久久无码精品亚洲日韩蜜臀浪潮 | 30岁的女人韩剧免费观看| 亚洲精品在线免费观看视频| 免费看片在线观看| 亚洲综合色丁香婷婷六月图片 | 亚洲天堂男人影院| 精品国产免费观看| 一级特黄aaa大片免费看| 亚洲国产婷婷六月丁香| 无码国产精品一区二区免费3p | 9久热这里只有精品免费| 91久久亚洲国产成人精品性色| 91手机看片国产永久免费| 无码亚洲成a人在线观看| 亚洲一区二区三区无码影院| 久久免费观看国产99精品| 亚洲剧场午夜在线观看| 免费人成网站7777视频| 成全高清在线观看免费| 中中文字幕亚洲无线码| 亚洲国产精品第一区二区三区| 亚洲中文字幕在线观看| 亚洲AV日韩精品一区二区三区| 亚洲中文字幕无码久久综合网| 亚洲精品线路一在线观看| 亚洲AV无码乱码国产麻豆穿越 | 久久福利青草精品资源站免费|