Goè¨èªææ³æ
åº
æè¿ãæµæ è¦å¯ãå ¼ããä»äºã¨ã¨ãã¦Goã§ã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¦ããããã®ã¢ããªã±ã¼ã·ã§ã³ãã©ããªãã®ãã¯ãã®ãã¡id:tagomorisãããã©ããã§è©±ãã¨æãããã©ããã®ã³ã³ãã¼ãã³ããOSSåãããäºå®ã¯ãã¾ã®ã¨ãããªãã®ã§ãããã§å¾ãç¥è¦ãããã«ã¾ã¨ãã¦ãããã¨ã«ããã
Goroutineã¨Channel
ãã¦ãGoã¨ããã°Goroutineã¨Channelã§ããã
Goroutineã¯ããããã«ã¹ã¬ãããªãã§ãããææ³ã¨å®è£ ã®æ¯æ´ã§ããæ°è»½ã«ä½¿ããã®ãä»ã®è¨èªã¨ã®éãã§ããããããªã®ã§ãGoroutineãã©ãã ãã»ãã»ã使ãã¹ããã¨ããã³ã¹ãæè¦ã身ã«ã¤ãããã¨ãã¨ã¦ã大äºãªæ°ããã¾ããRubyãªã©ã¨ã¯æ°æã¡ãåãæ¿ãã¦ããå¿ è¦ãããã§ããããã¼ãã¯ã¾ã åãæ¿ãããã¦ãã¾ããã
ããä¸ã¤ãChannelã§ãããããã¯ç«¯çã«ã¯ã¡ãã»ã¼ã¸ãã¥ã¼ã§ãã Goã¯åè¿°ã®éãåæã«åããã«ãã¹ã¬ãã (Simultaneous Multithreading; SMT) ãæ°è»½ã«æ±ããããã§ãããä¸ã¤ã®äººé¡æªã®ä¸ã¤ã§ãããã«ãã¹ã¬ããã«ä¸¸è °ã§ç«ã¡åãã£ã¦ãç¡æ®ãªæ»ãè¦ãã¦ãã¾ãã ãã®ããã®éå ·ã®ä¸ã¤ããã¥ã¼ã§ãé©åã«ããã¯ãç¨ãã¦ä½ãããéä¿¡è·¯ãçµç±ãã¦ãã¼ã¿ãã³ãã¼ãããã¨ã§ããããããããã¤æ¯è¼çå¹çããã¹ã¬ãããã¾ããã§ãã¼ã¿ãããã¨ããããã¨ãã§ããããã§ããï¼ã¡ãªã¿ã«Rubyã«ãThread::Queueã£ã¦ã®ããã£ãããã¾ãããCRubyã ã¨GVLãããã®ã§ãããã使ããã¨ã¯å°ãªãããï¼
ã¨ã¯è¨ã£ã¦ããããªãã«ã³ã¹ãããããã§ããã?ã£ã¦äººã¯chansendã®å®è£ ãçºããã¨ãã£ã³ãã«ãã¨ã®ããã¯ãåã£ã¦memcpyãã¦ãã ãã£ã¦ãããã®ã§ãé å¼µã£ã¦ä¸¦åããã·ã¥ã¨ã使ããªãã¦ãããã ã£ã¦æ°åã«ãªãã¾ããéåä¿¡ã«ãããæéã¯10~100ããç§ãããããª? ã¨ããã¨ã話ã¯ããã«æ¸ãããã¢ããªã±ã¼ã·ã§ã³ãGoroutineã«åå²ããéãChannelã§ã¤ãªããã¨ãã話ã«ãªãã¾ããããã¦ãæ§ã ãªãã¼ã¿ã¯åºæ¬çã«ããããã®Goroutineå ã«éãè¾¼ããä»ã®Goroutineã¯Channelçµç±ã§ã¢ã¯ã»ã¹ããã
ãã®æ§å³ãã©ããã§è¦ããã¨ãããã¾ããï¼ãããã¤ã¯ããµã¼ãã¹ã®è©±ã¨åãã§ãã Goã§ã¢ããªã±ã¼ã·ã§ã³æ¸ãã®ã¯ããã®ãããªãã¤ã¯ããµã¼ãã¹çãªã³ã³ãã¼ãã³ãåå²ã¨ãããã¯ããªæ¥½ãã¿ã¨ãCã®ã³ã¼ãã触ã£ã¦ããæã®ãããªç´°ããªãã¼ã¿ã®åãåãã¨ãããã¯ããªæ¥½ãã¿ãé£ãåããã«ãã£ã¦ãç¬ç¹ã®æè¦ãè¦ãã¾ãã
ãã¹ãã«ã¤ãã¦
Goã®æ¨æºããã±ã¼ã¸ã«ã¯testingã£ã¦ã®ãããã¾ãããã®ããã±ã¼ã¸ã®ææ³ã¯Go ã® Test ã«å¯¾ããèãæ¹ãªã©ã§è§£èª¬ããã¦ãã¾ãããã¾ããä¸çã¯ãããã©â¦â¦ãã¨ããææ³ã®æ¹ãå¤ãã®ã§ã¯ãªãã§ãããããããããã¡ã§ãããã¤ãããã£ã¦åå°çã«åå¿ããããªãã¾ãããæ¬æã表ãã¦å®éã«testingã§ãã£ã¦ã¿ã¾ããã
ããããã¡ã§ãããããçµå±ãã¹ãã®ã¡ãã»ã¼ã¸ãªãã¦âA is expected but Bâã9å²ã§ãããã®ã¡ãã»ã¼ã¸ããã¡ãã¡èããã¨ã決æåã®ç¡é§é£ãã§ãããæ¥æ¬äººãè±èªã§ã¡ãã»ã¼ã¸ã綴ããªã½ã¼ã¹ãæéã ã¨ããæ²ããç¾å®ãèæ ®ãã¦ããªããã¼ãã¯5åãããassertionãæ¸ããã¨ããã§å«ã«ãªã£ã¦ãã¨ã¯ã²ãããâA is expected but Bâãã³ãããã¾ããã¾ããã
ã¾ãã§ããããã§è´å½çã«ã¤ãããã¨ããã¨çé¢ç®ã«ããã®ãæ¾æ£ãã¦ã³ããããã°è¯ãããGoã®fmt.Printfã¯%vã¨%#vã便å©ãªã®ã§ããªãã¨ããªãã¯ãã¾ããassert_equalã®é¡ä»¥å¤ã«ãã¿ã¤ã ã¢ã¦ããã¤ãã¦ãã£ã³ãã«ãèªãã ãããassertionã¡ã½ããã欲ãããªã£ã¦ä½ããããªãããããã¾ããããããããã¨ãã¯runtime.Callerã§å¼ã³åºãå ã®è¡æ°ãåããã®ã§ããã¾ããã¨ããã¨ããã§ãã ã¾ã¨ããã¨ãæ£ç´testingã¯ãã¡ã ã¨æããã©ãã¨ã¯ãããªãã¨ãã¯ãªãã®ã§testingã§ä¾åå ãæ¸ããã®ã¯ããããã
ãã®ä»
- log.Fatalã¯ä½¿ããªã
- ã«ãã¬ãã¸ä¸ãã¥ãããªã
- fatalã«è¿½ãè¾¼ã¾ãããããªã¨ã©ã¼ã¯ãã£ã¨ä¸æµã§æãã¹ããªãã ã¨æã
- ãã¹ãã§logãéªéãªã¨ãã¯log.SetOutput(ioutil.Discard)ã§é»ããã
- 並åå®è¡ã§ãã¾ããããªãã®ã§loggerãæããã¦åå¥ã«ãã£ãæ¹ããã¶ããã
- å°ã£ãã¨ãã¯goto â ãã¾ãã»ãã¨ãã«ããã§ããã®ã
- switchã¹ãã â ãã¾ãã»ãã¨ãã«ããã§ããã®ã
- selectãswitchã®ä¸ã§breakããã¨å¤å´ã®forã¾ã§å±ããªãã®ã§break <label>ããã°ãããã©ãçµå±goto使ã
- Printf(â%#vâ, v)ã¹ãã
- errorä¼æããããã¨ãã¯ãpkg/errorsã®cause使ã
- ã¨ããã¨ãã¨ã©ã¼ç¨æ§é ä½ã¯ä½ãã¾ãã£ãæ¹ããã â æ¬å½ã«ï¼
- sync/atomicã¨ããããã©ãnew(expvar.Int)ã¨ãã§mutexä»ãå¤æ°ãç°¡åã«ä½ããã便å©
- Goã¯nullå®å ¨ã§ã¯ãªãâæ§é ä½ã®ãã¤ã³ã¿ãæ±ãå§ããã¨æ°ã«ãªã£ã¦ãã
ã¾ã¨ã
ãã¼ã£ã¨ããã®è¨äºã®è¶£æ¨ã¯ãGoã¯21ä¸ç´ã®Rubyistã触ã£ã¦ããã¹ãè¨èªãªã®ãï¼ãã§ããã£ããElixirã¨ãããã£ã¦ããªãã®ãªããGuildã®äºç¿ã¨ãã¦ä¸åº¦Goã§ä½ãä½ã£ã¦ã¿ãã¨ããã¨æãã¾ãããä½ãã¨è¨ã£ã¦ãããããæªãä¾ãããªãã¦channelã使ã£ãã¢ããªãä½ã£ã¦ã¿ãã¨ãå¾æ¥ã®ã¹ã¬ãããããããããæ´ããªãã大äºãªå ±æãªã½ã¼ã¹ãããã¯ã§å®ãã¿ãããªã®ã¨ã¯éãè¨è¨ãä½ã«æã¿ããã¾ããã¿ãªãããGoãããã£ã¦Ruby 3.0ãæºåä¸ç«¯ã§è¿ããããããã«ãã¾ãããï¼
Re: Re: Goè¨èªææ³æ
mattnããã®Re: Goè¨èªææ³æã«ã¤ãã¦ã
ãã¹ãã«ã¤ãã¦
ã¢ãµã¼ã·ã§ã³ãã³ãã¼ããªããã°ãªããªãçç±ã¯ä¸ã¤ã®ãã¹ãã±ã¼ã¹ã®ä¸ã§ç°ãªããã¹ããæ··å¨ãã¦ããããåã«åæ§ã®ãã¹ããã³ãã¼ãã¦ä½ããã¦ããã®ãåå ã§ã¯ãªãã§ããããã
ãã¼ãã¡ãã£ã¨è¨èãæªãã£ãããªããããã¯ããããmattnãããã10åãããDRYã«é¢ãã¦çæ°ã§ãmattnããã®æããä¾ã§è¨ãã°ã以ä¸ã®3è¡ã§ãã許ããªãã®ã§ãã
if !test.err && err != nil { t.Fatalf("should not be error for %v but:", test.input, err) } if test.err && err == nil { t.Fatalf("should be error for %v but not:", test.input) } if got != test.want { t.Fatalf("want %q, but %q:", test.want, got) }
ãã¨ã¯ãTable Driven Testsã£ã¦åºæ¬çã«ã¯é¢æ°çã¨ãããå ¥åã¨åºåã対å¿ãããã®ã§ã¯ä¾¿å©ãªãã ãã©ãREST APIãå©ãã³ã¼ãã®ãªãã©ã¤é¨åã®ãã¹ãã¨ããç¶æ ãæã£ã¦ä»ã®ã³ã³ãã¼ãã³ãã¨channelã§éä¿¡ããªããã«ã¼ãããã³ã³ãã¼ãã³ãã®ãã¹ãã«ã¯ãã¾ãã¡ã
ãã®ä»
Golang ã«éããã§ãããæè¿ã®è¨èªã§ã¯ Labeled Break ã¨ããç©ãããã¾ãã
ããã´ã¡ã³ãselectãswitchã®ä¸ã§breakããã¨å¤å´ã®forã¾ã§å±ããªãã®ã§break <label>ããã°ãããã©ãçµå±goto使ããã®<label>ãã¿ã°æ±ãã«ãªã£ã¦æ¶ãã¦ãâ¦â¦ãããã¯ããã¨ãã¦ãä¸è¿°ã®ãªãã©ã¤ã¿ãããªã±ã¼ã¹ã ã¨çµå±goto使ãã¾ããã
nullå®å ¨ã«ã¤ãã¦
ããã¯ä¸éä¸è¬ã§ã®æå³ã§ã®ãnullå®å ¨ãã§ãããã¤ã³ã¿ã§ã¯ãªãç´æ¥æ§é ä½ãæ±ãã°ãã¾ããã¨è¡ãããªã¼ã¨æãããããªãããããã§ãã¤ã³ã¿ä½¿ãç¾½ç®ã«ãªã£ãããã¦æ²ããã