Goèšèªã§ãããã¡ãªééã
The Coder Cafe
ããç§ã®æ¬ã楜ããã§ããã ãããªããç§ã®ææ°ãããžã§ã¯ãã«ããèå³ããããããããŸãããThe Coder Cafeã¯ãã³ãŒããŒåãã®æ¥åãã¥ãŒã¹ã¬ã¿ãŒã§ãã
Feeling overwhelmed by the endless stream of tech content? At The Coder Cafe, we serve one essential concept for coders daily. Written by a senior software engineer at Google, it's perfectly brewed for your morning coffee, helping you grow your skills deeply.
ãã®ããŒãžã¯ã100 Go Mistakesãã®å 容ããŸãšãããã®ã§ããäžæ¹ã§ãã³ãã¥ããã£ã«éãããããŒãžã§ããããŸããããããã¡ãªééãããæ°ãã«è¿œå ãããã¹ãã ãšãèãã§ããã community mistake issue ãäœæããŠãã ããã
泚æ
çŸåšãå€§å¹ ã«å€ãã®ã³ã³ãã³ããè¿œå ããŠåŒ·åããŠããæ°ããããŒãžã§ã³ãé²èŠ§ããŠããŸãããã®ããŒãžã§ã³ã¯ãŸã éçºäžã§ããåé¡ãèŠã€ããå Žåã¯ã©ããæ°è»œã«PRãäœæããŠãã ããã
ã³ãŒããšãããžã§ã¯ãæ§æ
æå³çã§ãªãå€æ°ã®ã·ã£ããŒã€ã³ã° (#1)
èŠçŽ
å€æ°ã®ã·ã£ããŒã€ã³ã°ãé¿ããããšã¯ã誀ã£ãå€æ°ã®åç §ãèªã¿æã®æ··ä¹±ãé²ããŸãã
å€æ°ã®ã·ã£ããŒã€ã³ã°ã¯ãå€æ°åããããã¯å
ã§å宣èšãããããšã§çããŸãããããã¯ééããåŒãèµ·ãããããããŸããå€æ°ã®ã·ã£ããŒã€ã³ã°ãçŠæ¢ãããã©ããã¯å人ã®å¥œã¿ã«ãããŸããããšãã°ããšã©ãŒã«å¯Ÿã㊠err
ã®ãããªæ¢åã®å€æ°åãåå©çšãããšäŸ¿å©ãªå ŽåããããŸãããšã¯ãããã³ãŒãã¯ã³ã³ãã€ã«ããããã®ã®ãå€ãåãåã£ãå€æ°ãäºæãããã®ã§ã¯ãªããšããã·ããªãªã«çŽé¢ããå¯èœæ§ããããããååãšããŠåŒãç¶ã泚æãæãå¿
èŠããããŸãã
äžå¿ èŠã«ãã¹ããããã³ãŒã (#2)
èŠçŽ
ãã¹ããæ·±ããªããªãããã«ããããããŒãã¹ãå·ŠåŽã«æããããšã§ã¡ã³ã¿ã«ã³ãŒãã¢ãã«ãæ§ç¯ããããšã容æã«ãªããŸãã
äžè¬çã«ãé¢æ°ãããæ·±ããã¹ããèŠæ±ããã»ã©ãèªãã§ç解ããããšãããè€éã«ãªããŸããç§ãã¡ã®ã³ãŒãã®å¯èªæ§ãæé©åããããã«ããã®ã«ãŒã«ã®é©çšæ¹æ³ãèŠãŠãããŸãããã
if
ãããã¯ãè¿ããããšãããã¹ãŠã®å Žåã«ãããŠelse
ãããã¯ãçç¥ããå¿ èŠããããŸãã ããšãã°ã次ã®ããã«æžãã¹ãã§ã¯ãããŸããã
代ããã«ã次ã®ããã« else
ãããã¯ãçç¥ããŸãã
- ãã³ããããŒãã¹ã§ããã®ããžãã¯ã«åŸãããšãå¯èœã§ãã
ããã§ã¯ã空㮠s
ããã³ããããŒãã¹ãè¡šããŸãããããã£ãŠã次ã®ããã«æ¡ä»¶ãã²ã£ããè¿ãå¿
èŠããããŸãã
èªã¿ãããã³ãŒããæžãããšã¯ããã¹ãŠã®éçºè ã«ãšã£ãŠéèŠãªèª²é¡ã§ãããã¹ãããããããã¯ã®æ°ãæžããããåªããããããŒãã¹ãå·ŠåŽã«æããã§ããã ãæ©ãæ»ãããšããã³ãŒãã®å¯èªæ§ãåäžãããå ·äœçãªæ段ã§ãã
inité¢æ°ã®èª€çš (#3)
èŠçŽ
å€æ°ãåæåãããšãã¯ãinité¢æ°ã®ãšã©ãŒåŠçãå¶éãããŠãããã¹ããŒãã®åŠçãšãã¹ããããè€éã«ãªãããšã«æ³šæããŠãã ãããã»ãšãã©ã®å Žåãåæåã¯ç¹å®ã®é¢æ°ãšããŠåŠçãããã¹ãã§ãã
inité¢æ°ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã¹ããŒããåæåããããã«äœ¿çšãããé¢æ°ã§ããåŒæ°ãåãããçµæãè¿ããŸããïŒ func()
é¢æ°ïŒãããã±ãŒãžãåæåããããšãããã±ãŒãžå
ã®ãã¹ãŠã®å®æ°ããã³å€æ°ã®å®£èšãè©äŸ¡ãããŸãã次ã«ãinité¢æ°ãå®è¡ãããŸãã
inité¢æ°ã¯ããã€ãã®åé¡ãåŒãèµ·ããå¯èœæ§ããããŸãã
- ãšã©ãŒåŠçãå¶éãããå¯èœæ§ããããŸãã
- ãã¹ãã®å®è£ æ¹æ³ãè€éã«ãªãå¯èœæ§ããããŸãïŒããšãã°ãå€éšäŸåé¢ä¿ãèšå®ããå¿ èŠããããŸãããåäœãã¹ãã®ç¯å²ã§ã¯å¿ èŠãªãå¯èœæ§ããããŸãïŒã
- åæåã§ã¹ããŒããèšå®ããå¿ èŠãããå Žåã¯ãã°ããŒãã«å€æ°ã䜿çšããŠè¡ãå¿ èŠããããŸãã
inité¢æ°ã«ã¯æ³šæãå¿ èŠã§ãããã ããéçæ§æã®å®çŸ©ãªã©ãç¶æ³ã«ãã£ãŠã¯åœ¹ç«ã€å ŽåããããŸãããã以å€ã®ã»ãšãã©ã®å ŽåãåæååŠçã¯ç¹å®ã®é¢æ°ãéããŠè¡ãããã¹ãã§ãã
ã²ãã¿ãŒãšã»ãã¿ãŒã®ä¹±çš (#4)
èŠçŽ
Goèšèªã§ã¯ãæ £çšçã«ã²ãã¿ãŒãšã»ãã¿ãŒã®äœ¿çšã匷å¶ããããšã¯ãããŸãããå®å©ãéèŠããå¹çæ§ãšç¹å®ã®æ £ç¿ã«åŸãããšãšã®éã®é©åãªãã©ã³ã¹ãèŠã€ããããšããé²ãã¹ãéã§ããã¯ãã§ãã
ããŒã¿ã®ã«ãã»ã«åãšã¯ããªããžã§ã¯ãã®å€ãŸãã¯ç¶æ ãé ãããšãæããŸããã²ãã¿ãŒãšã»ãã¿ãŒã¯ããšã¯ã¹ããŒããããŠããªããªããžã§ã¯ããã£ãŒã«ãã®äžã«ãšã¯ã¹ããŒããããã¡ãœãããæäŸããããšã§ã«ãã»ã«åãå¯èœã«ããæ段ã§ãã
Goèšèªã§ã¯ãäžéšã®èšèªã§èŠããããããªã²ãã¿ãŒãšã»ãã¿ãŒã®èªåãµããŒãã¯ãããŸããããŸããã²ãã¿ãŒãšã»ãã¿ãŒã䜿çšããŠæ§é äœãã£ãŒã«ãã«ã¢ã¯ã»ã¹ããããšã¯å¿ é ã§ãæ £çšçã§ããããŸãããå€ããããããªãæ§é äœã®ã²ãã¿ãŒãšã»ãã¿ãŒã§ã³ãŒããåããã¹ãã§ã¯ãããŸãããå®å©ãéèŠããä»ã®ããã°ã©ãã³ã°ãã©ãã€ã ã§æã«ã¯è°è«ã®äœå°ããªããšèããããŠããæ £ç¿ã«åŸãããšãšãå¹çæ§ãšã®éã®é©åãªãã©ã³ã¹ãèŠã€ããããåªããã¹ãã§ãã
Goèšèªã¯ãã·ã³ãã«ããå«ãå€ãã®ç¹æ§ãèæ ®ããŠèšèšãããç¬èªã®èšèªã§ããããšãå¿ããªãã§ãã ããããã ããã²ãã¿ãŒãšã»ãã¿ãŒã®å¿ èŠæ§ãèŠã€ãã£ãå ŽåããŸãã¯åè¿°ã®ããã«ãåæ¹äºææ§ãä¿èšŒããªããå°æ¥ã®å¿ èŠæ§ãäºæž¬ãããå Žåã¯ããããã䜿çšããããšã«åé¡ã¯ãããŸããã
ã€ã³ã¿ãã§ãŒã¹æ±æ (#5)
èŠçŽ
æœè±¡åã¯äœæãããã¹ããã®ã§ã¯ãªããçºèŠãããã¹ããã®ã§ããäžå¿ èŠãªè€éããé¿ããããã«ãã€ã³ã¿ãã§ãŒã¹ã¯ãå¿ èŠã«ãªããšäºæž¬ãããšãã§ã¯ãªããå¿ èŠã«ãªã£ããšãã«äœæããããå°ãªããšãæœè±¡åãæå¹ã§ããããšã蚌æã§ããå Žåã«äœæããŠãã ããã
ã€ã³ã¿ãã§ãŒã¹ã¯ããªããžã§ã¯ãã®åäœãæå®ããæ¹æ³ãæäŸããŸããè€æ°ã®ãªããžã§ã¯ããå®è£
ã§ããå
±éé
ãæœåºããããã«ãã€ã³ã¿ãã§ãŒã¹ã¯äœ¿çšãããŸããGoèšèªã®ã€ã³ã¿ãã§ãŒã¹ã倧ããç°ãªãã®ã¯ãæé»çã«æºããããããšã§ãããªããžã§ã¯ã X
ãã€ã³ã¿ãã§ãŒã¹ Y
ãå®è£
ããŠããããšã瀺ã implements
ã®ãããªæ瀺çãªããŒã¯ãŒãã¯ãããŸããã
äžè¬ã«ãã€ã³ã¿ãã§ãŒã¹ã䟡å€ããããããšèããããäž»èŠãªäœ¿çšäŸã¯ïŒã€ãããŸããããã¯ãå ±éã®åäœãé€å€ãããäœããã®åé¢ãäœæãããããã³åãç¹å®ã®åäœã«å¶éãããšãããã®ã§ãããã ãããã®ãªã¹ãã¯ãã¹ãŠãç¶²çŸ ããŠããããã§ã¯ãªããçŽé¢ããç¶æ³ã«ãã£ãŠãç°ãªããŸãã
å€ãã®å Žåãã€ã³ã¿ãã§ãŒã¹ã¯æœè±¡åããããã«äœæãããŸãããããŠãããã°ã©ãã³ã°ã§æœè±¡åãããšãã®äž»ãªæ³šæç¹ã¯ãæœè±¡åã¯äœæãããã¹ãã§ã¯ãªããçºèŠãããã¹ãã§ãããšããããšãèŠããŠããããšã§ããããªãã¡ãããããçŽæ¥ã®çç±ããªãéããã³ãŒãå ã§æœè±¡åãã¹ãã§ã¯ãªããšããããšã§ããã€ã³ã¿ãã§ãŒã¹ã䜿ã£ãŠèšèšããã®ã§ã¯ãªããå ·äœçãªããŒãºãåŸ ã€ã¹ãã§ããå¥ã®èšãæ¹ãããã°ãã€ã³ã¿ãã§ãŒã¹ã¯å¿ èŠã«ãªããšäºæž¬ãããšãã§ã¯ãªããå¿ èŠã«ãªã£ããšãã«äœæããå¿ èŠããããŸãã ã€ã³ã¿ãã§ãŒã¹ã®é床ãªäœ¿çšãããå Žåã®äž»ãªåé¡ã¯äœã§ãããããçãã¯ãã³ãŒããããŒãããè€éã«ãªãããšã§ãã圹ã«ç«ããªãéæ¥åç §ãè¿œå ããŠãäœã®äŸ¡å€ããããŸãããããã¯äŸ¡å€ã®ãªãæœè±¡åãããããšã§ãã³ãŒããèªã¿ãç解ããæšè«ããããšãããã«å°é£ã«ããŸããã€ã³ã¿ãã§ãŒã¹ãè¿œå ããæ確ãªçç±ããªããã€ã³ã¿ãã§ãŒã¹ã«ãã£ãŠã³ãŒããã©ã®ããã«æ¹åãããããäžæçãªå Žåã¯ããã®ã€ã³ã¿ãã§ãŒã¹ã®ç®çã«ç°è°ãå±ããå¿ èŠããããŸããå®è£ ãçŽæ¥åŒã³åºãã®ãäžã€ã®æã§ãã
ã³ãŒãå ã§æœè±¡åãããšãã¯æ³šæãå¿ èŠã§ãïŒæœè±¡åã¯äœæããã®ã§ã¯ãªããçºèŠããå¿ èŠããããŸãïŒãåŸã§å¿ èŠã«ãªãå¯èœæ§ããããã®ãèæ ®ããå®ç§ãªæœè±¡åã¬ãã«ãæšæž¬ããŠãç§ãã¡ãœãããŠã§ã¢éçºè ã¯ã³ãŒãããªãŒããŒãšã³ãžãã¢ãªã³ã°ããããšããããããŸããã»ãšãã©ã®å Žåãã³ãŒããäžå¿ èŠãªæœè±¡åã§æ±æãããèªã¿ã«ãããªãããããã®ããã»ã¹ã¯é¿ããã¹ãã§ãã
ããã»ãã€ã¯
ã€ã³ã¿ãã§ãŒã¹ã§ãã¶ã€ã³ãããªãã€ã³ã¿ãã§ãŒã¹ãèŠã€ãåºãã
æœè±¡çã«åé¡ã解決ããããšããã®ã§ã¯ãªããä»è§£æ±ºãã¹ãããšã解決ããŸããããæåŸã«éèŠãªããšã§ãããã€ã³ã¿ãã§ãŒã¹ã«ãã£ãŠã³ãŒããã©ã®ããã«æ¹åãããããäžæçãªå Žåã¯ãã³ãŒããç°¡çŽ åããããã«ã€ã³ã¿ãã§ãŒã¹ãåé€ããããšãæ€èšããå¿ èŠãããã§ãããã
çç£è åŽã®ã€ã³ã¿ãã§ãŒã¹ (#6)
èŠçŽ
ã€ã³ã¿ãã§ãŒã¹ãã¯ã©ã€ã¢ã³ãåŽã§ä¿æããããšã§äžå¿ èŠãªæœè±¡åãåé¿ã§ããŸãã
Goèšèªã§ã¯ã€ã³ã¿ãã§ãŒã¹ãæé»çã«æºããããŸããããã¯ãæ瀺çãªå®è£ ãæã€èšèªãšæ¯èŒããŠå€§ããªå€åãããããåŸåããããŸããã»ãšãã©ã®å ŽåãåŸãã¹ãã¢ãããŒãã¯åã®ã»ã¯ã·ã§ã³ã§èª¬æãããã®ââæœè±¡åã¯äœæããã®ã§ã¯ãªããçºèŠããå¿ èŠãããââã«äŒŒãŠããŸããããã¯ããã¹ãŠã®ã¯ã©ã€ã¢ã³ãã«å¯ŸããŠç¹å®ã®æœè±¡åã匷å¶ããã®ã¯çç£è ã®åœ¹å²ã§ã¯ãªãããšãæå³ããŸãã代ããã«ãäœããã®åœ¢åŒã®æœè±¡åãå¿ èŠãã©ãããå€æãããã®ããŒãºã«æé©ãªæœè±¡åã¬ãã«ã決å®ããã®ã¯ã¯ã©ã€ã¢ã³ãã®è²¬ä»»ã§ãã
ã»ãšãã©ã®å Žåãã€ã³ã¿ãã§ãŒã¹ã¯æ¶è²»è åŽã«ååšããå¿ èŠããããŸãããã ããç¹å®ã®ç¶æ³ïŒããšãã°ãæœè±¡åãæ¶è²»è ã«ãšã£ãŠåœ¹ç«ã€ããšãããã£ãŠããââäºæž¬ã¯ããŠããªãââå ŽåïŒã§ã¯ããããçç£è åŽã§äœ¿çšãããå ŽåããããŸããããããå Žåãå¯èœãªéãæå°éã«æããåå©çšå¯èœæ§ãé«ããããç°¡åã«æ§æã§ããããã«åªããã¹ãã§ãã
ã€ã³ã¿ãã§ãŒã¹ãè¿ã (#7)
èŠçŽ
æè»æ§ã«åé¡ããªãããã«ããããã«ãé¢æ°ã¯ã»ãšãã©ã®å Žåãã€ã³ã¿ãã§ãŒã¹ã§ã¯ãªãå ·äœçââãªå®è£ ãè¿ãå¿ èŠããããŸããéã«ãé¢æ°ã¯å¯èœãªéãã€ã³ã¿ãã§ãŒã¹ãåãå ¥ããå¿ èŠããããŸãã
ã»ãšãã©ã®å Žåãã€ã³ã¿ãã§ãŒã¹ã§ã¯ãªãå ·äœçãªå®è£ ãè¿ãå¿ èŠããããŸããããã§ãªããšããã±ãŒãžã®äŸåé¢ä¿ã«ããèšèšããã£ããè€éã«ãªãããã¹ãŠã®ã¯ã©ã€ã¢ã³ããåãæœè±¡åã«äŸåããå¿ èŠããããããæè»æ§ã«æ¬ ããå¯èœæ§ããããŸããçµè«ã¯åã®ã»ã¯ã·ã§ã³ãšäŒŒãŠããŸããæœè±¡åãã¯ã©ã€ã¢ã³ãã«ãšã£ãŠåœ¹ç«ã€ããšãïŒäºæž¬ãããã§ã¯ãªãïŒããã£ãŠããå Žåã¯ãã€ã³ã¿ãã§ãŒã¹ãè¿ãããšãæ€èšããŠãããã§ãããããã以å€ã®å Žåã¯ãæœè±¡åã匷å¶ãã¹ãã§ã¯ãããŸããããããã¯ã¯ã©ã€ã¢ã³ãã«ãã£ãŠçºèŠãããå¿ èŠããããŸããäœããã®çç±ã§ã¯ã©ã€ã¢ã³ããå®è£ ãæœè±¡åããå¿ èŠãããå Žåã§ããã¯ã©ã€ã¢ã³ãåŽã§ãããè¡ãããšãã§ããŸãã
any
ã¯äœãèšããªã (#8)
èŠçŽ
json.Marshal
ãªã©èããããã¹ãŠã®åãåãå
¥ãããè¿ãå¿
èŠãããå Žåã«ã®ã¿ any
ã䜿çšããŠãã ããããã以å€ã®å Žåãany
ã¯æå³ã®ããæ
å ±ãæäŸãããåŒã³åºãå
ãä»»æã®ããŒã¿åã®ã¡ãœãããåŒã³åºãããšãèš±å¯ãããããã³ã³ãã€ã«æã«åé¡ãçºçããå¯èœæ§ããããŸãã
any
åã¯ãèããããã¹ãŠã®åãåãå
¥ãããè¿ãå¿
èŠãããå ŽåïŒããšãã°ãããŒã·ã£ãªã³ã°ããã©ãŒãããã®å ŽåïŒã«åœ¹ç«ã¡ãŸããååãšããŠã³ãŒããé床ã«äžè¬åããããšã¯äœãšããŠãé¿ããã¹ãã§ããã³ãŒãã®è¡šçŸåãªã©ã®ä»ã®åŽé¢ãåäžããå Žåã¯ãã³ãŒããå°ãéè€ãããã»ããè¯ãããšããããŸãã
ãžã§ããªãã¯ã¹ããã€äœ¿çšããã¹ããç解ããŠããªã (#9)
èŠçŽ
ãžã§ããªãã¯ã¹ãšåãã©ã¡ãŒã¿ãŒãå©çšããããšã§ãèŠçŽ ãåäœãé€å€ããããã®ãã€ã©ãŒãã¬ãŒãã³ãŒããé¿ããããšãã§ããŸãããã ããåãã©ã¡ãŒã¿ã¯ææå°æ©ã«äœ¿çšãããå ·äœçãªå¿ èŠæ§ãããã£ãå Žåã«ã®ã¿äœ¿çšããŠãã ãããããã§ãªããã°ãäžå¿ èŠãªæœè±¡åãšè€éããçããŸãã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
åã®åã蟌ã¿ã§èµ·ããããåé¡ãææ¡ããŠããªã (#10)
èŠçŽ
ååã蟌ã¿ã䜿çšãããšããã€ã©ãŒãã¬ãŒãã³ãŒããåé¿ããããšãã§ããŸãããã ããããããããšã§ãäžéšã®ãã£ãŒã«ããé衚瀺ã«ããŠããå¿ èŠãããå Žåã«åé¡ãçºçããªãããã«ããŠãã ããã
æ§é äœãäœæãããšããGoèšèªã¯åãåã蟌ããªãã·ã§ã³ãæäŸããŸãããã ããååã蟌ã¿ã®æå³ããã¹ãŠç解ããŠããªããšãäºæ³å€ã®åäœãçºçããå¯èœæ§ããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãåãåã蟌ãæ¹æ³ãããããããããã®ãããã³èããããåé¡ã«ã€ããŠèŠãŠãããŸãã
Goèšèªã§ã¯ãååãªãã§å®£èšãããæ§é äœãã£ãŒã«ãã¯ãåã蟌ã¿ãšåŒã°ããŸããããšãã°ã次ã®ãããªãã®ã§ãã
Foo
æ§é äœã§ã¯ãBar
åãé¢é£ä»ããããååãªãã§å®£èšãããŠããŸãããããã£ãŠãããã¯åã蟌ã¿ãã£ãŒã«ãã§ãã
åã蟌ã¿ã䜿çšããããšã§ãåã蟌ã¿åã®ãã£ãŒã«ããšã¡ãœããã¯ææ ŒããŸããBar ã«ã¯ Baz ãã£ãŒã«ããå«ãŸããŠããããããã®ãã£ãŒã«ã㯠Foo
ã«ææ ŒããŸãããããã£ãŠãFoo ãã Baz ãå©çšã§ããããã«ãªããŸãã
åã®åã蟌ã¿ã«ã€ããŠäœãèšããã§ããããããŸãããããå¿ èŠã«ãªãããšã¯ã»ãšãã©ãªãããŠãŒã¹ã±ãŒã¹ãäœã§ãããããããååã蟌ã¿ãªãã§ãåæ§ã«è§£æ±ºã§ããããšãæå³ããŸããåã®åã蟌ã¿ã¯äž»ã«å©äŸ¿æ§ãç®çãšããŠäœ¿çšãããŸããã»ãšãã©ã®å Žåãããã¯åäœãææ Œããããã«äœ¿çšãããŸãã
ååã蟌ã¿ã䜿çšããå Žåã¯ã次㮠2 ã€ã®äž»ãªå¶çŽã念é ã«çœ®ãå¿ èŠããããŸãã
- ãã£ãŒã«ããžã®ã¢ã¯ã»ã¹ãç°¡çŽ åããããã®ç³è¡£æ§æãšããŠã®ã¿äœ¿çšããªãã§ãã ããïŒ
Foo.Bar.Baz()
ã®ä»£ããã«Foo.Baz()
ãªã©ïŒã ãããå¯äžã®æ ¹æ ã§ããå Žåã¯ãå éšåãåã蟌ãŸãã代ããã«ãã£ãŒã«ãã䜿ããŸãããã - å€éšããé ãããããŒã¿ïŒãã£ãŒã«ãïŒãåäœïŒã¡ãœããïŒãææ ŒããŠã¯ãªããŸãããããšãã°ãæ§é äœã«å¯ŸããŠãã©ã€ããŒããªãŸãŸã«ããŠããå¿ èŠãããããã¯åäœã«ã¯ã©ã€ã¢ã³ããã¢ã¯ã»ã¹ã§ããããã«ããå Žåãªã©ã§ãã
ãããã®å¶çŽã念é ã«çœ®ããŠååã蟌ã¿ãæèçã«äœ¿çšãããšãè¿œå ã®è»¢éã¡ãœããã«ãããã€ã©ãŒãã¬ãŒãã³ãŒããåé¿ããã®ã«åœ¹ç«ã¡ãŸãããã ããèŠãç®ã ããç®çãšããããé ãã¹ãèŠçŽ ãææ Œãããããªãããã«æ³šæããŸãããã
Functional Options ãã¿ãŒã³ã䜿çšããŠããªã (#11)
èŠçŽ
API ã«é©ããæ¹æ³ã§ãªãã·ã§ã³ã䟿å©ã«åŠçããã«ã¯ãFunctional Options ãã¿ãŒã³ã䜿çšããŸãããã
ããŸããŸãªå®è£ æ¹æ³ãååšããå€å°ã®éãã¯ãããŸãããäž»ãªèãæ¹ã¯æ¬¡ã®ãšããã§ãã
- æªãšã¯ã¹ããŒãã®æ§é äœã¯ãªãã·ã§ã³èšå®ãä¿æããŸãã
- åãªãã·ã§ã³ã¯åãåã
type Option func(options *options)
ãšã©ãŒãè¿ãé¢æ°ã§ããããšãã°ãWithPort
ã¯ããŒããè¡šãint
åŒæ°ãåãåããoptions
æ§é äœã®æŽæ°æ¹æ³ãè¡šãOption
åãè¿ããŸãã
type options struct {
port *int
}
type Option func(options *options) error
func WithPort(port int) Option {
return func(options *options) error {
if port < 0 {
return errors.New("port should be positive")
}
options.port = &port
return nil
}
}
func NewServer(addr string, opts ...Option) ( *http.Server, error) {
var options options
for _, opt := range opts {
err := opt(&options)
if err != nil {
return nil, err
}
}
// ãã®æ®µéã§ãoptions æ§é äœãæ§ç¯ãããæ§æãå«ãŸããŸãã
// ãããã£ãŠãããŒãèšå®ã«é¢é£ããããžãã¯ãå®è£
ã§ããŸãã
var port int
if options.port == nil {
port = defaultHTTPPort
} else {
if *options.port == 0 {
port = randomPort()
} else {
port = *options.port
}
}
// ...
}
Functional Options ãã¿ãŒã³ã¯ããªãã·ã§ã³ãåŠçããããã®æ軜㧠API ãã¬ã³ããªãŒãªæ¹æ³ãæäŸããŸãã Builder ãã¿ãŒã³ã¯æå¹ãªãªãã·ã§ã³ã§ãããããã€ãã®å°ããªæ¬ ç¹ïŒç©ºã®å¯èœæ§ãããæ§ææ§é äœãæž¡ããªããã°ãªããªãããŸãã¯ãšã©ãŒãåŠçããæ¹æ³ãããŸã䟿å©ã§ã¯ãªãïŒãããããã®çš®ã®åé¡ã«ãã㊠Functional Options ãã¿ãŒã³ãGoèšèªã«ãããæ £çšçãªå¯ŸåŠæ¹æ³ã«ãªãåŸåããããŸãã
誀ã£ããããžã§ã¯ãæ§æ (ãããžã§ã¯ãæ§é ãšããã±ãŒãžæ§æ) (#12)
å šäœçãªæ§æã«é¢ããŠã¯ãããŸããŸãªèãæ¹ããããŸããããšãã°ãã¢ããªã±ãŒã·ã§ã³ãã³ã³ããã¹ãããšã«æŽçãã¹ããããããšãã¬ã€ã€ãŒããšã«æŽçãã¹ãããããã¯å¥œã¿ã«ãã£ãŠç°ãªããŸããã³ã³ããã¹ãïŒé¡§å®¢ã³ã³ããã¹ããå¥çŽã³ã³ããã¹ããªã©ïŒããšã«ã³ãŒããã°ã«ãŒãåããããšãéžã¶å Žåãããã°ãå è§åœ¢ã®ã¢ãŒããã¯ãã£ååã«åŸãããšãšãæè¡å±€ããšã«ã°ã«ãŒãåããããšãéžã¶å ŽåããããŸããç§ãã¡ãè¡ã決å®ãäžè²«ããŠããéããããããŠãŒã¹ã±ãŒã¹ã«é©åãããªãããããééã£ãŠããããšã¯ãããŸããã
ããã±ãŒãžã«é¢ããŠã¯ãåŸãã¹ããã¹ããã©ã¯ãã£ã¹ãè€æ°ãããŸãããŸãããããžã§ã¯ããé床ã«è€éã«ãªãå¯èœæ§ããããããææå°æ©ãªããã±ãŒãžåã¯é¿ããã¹ãã§ããå Žåã«ãã£ãŠã¯ãå®ç§ãªæ§é ãæåããç¡çã«äœãããšããããããåçŽãªæ§æã䜿çšãããã®å 容ãç解ããäžã§ãããžã§ã¯ããçºå±ãããã»ããè¯ãå ŽåããããŸãã ç²åºŠãèæ ®ãã¹ãéèŠãªç¹ã§ãã 1 ã€ãŸã㯠2 ã€ã®ãã¡ã€ã«ã ããå«ãæ°åã®ããããã±ãŒãžãäœæããããšã¯é¿ããã¹ãã§ãããã®å Žåããããããããã®ããã±ãŒãžéã®è«ççãªæ¥ç¶ã®äžéšãæãèœã¡ãèªã¿æã«ãšã£ãŠãããžã§ã¯ããç解ãã«ãããªãããã§ããéã«ãããã±ãŒãžåã®æå³ãèãããããªå·šå€§ãªããã±ãŒãžãé¿ããã¹ãã§ãã
ããã±ãŒãžã®ååä»ãã泚æããŠè¡ãå¿ èŠããããŸããïŒéçºè ãªãïŒèª°ããç¥ã£ãŠããããã«ãååãä»ããã®ã¯é£ããã§ããã¯ã©ã€ã¢ã³ãã Go ãããžã§ã¯ããç解ããããããã«ãããã±ãŒãžã«å«ãŸãããã®ã§ã¯ãªããæäŸãããã®ã«åºã¥ããŠããã±ãŒãžã«ååãä»ããå¿ èŠããããŸãããŸããããŒãã³ã°ã«ã¯æå³ã®ãããã®ãä»ããå¿ èŠããããŸãããããã£ãŠãããã±ãŒãžåã¯çããç°¡æœã§ãè¡šçŸåè±ãã§ãæ £äŸã«ããåäžã®å°æåã«ããå¿ èŠããããŸãã
äœããšã¯ã¹ããŒããããã«ã€ããŠã®ã«ãŒã«ã¯éåžžã«ç°¡åã§ããããã±ãŒãžéã®çµåãæžããããšã¯ã¹ããŒããããäžèŠãªèŠçŽ ãé衚瀺ã«ããããã«ããšã¯ã¹ããŒãããå¿ èŠããããã®ãã§ããéãæå°éã«æããå¿ èŠããããŸããèŠçŽ ããšã¯ã¹ããŒããããã©ããäžæãªå Žåã¯ãããã©ã«ãã§ãšã¯ã¹ããŒãããªãããã«ããå¿ èŠããããŸããåŸã§ãšã¯ã¹ããŒãããå¿ èŠãããããšãå€æããå Žåã¯ãã³ãŒãã調æŽã§ããŸãããŸããæ§é äœã encoding/json ã§ã¢ã³ããŒã·ã£ãªã³ã°ã§ããããã«ãã£ãŒã«ãããšã¯ã¹ããŒããããªã©ãããã€ãã®äŸå€ã«ãçæããŠãã ããã
ãããžã§ã¯ããæ§æããã®ã¯ç°¡åã§ã¯ãããŸãããããããã®ã«ãŒã«ã«åŸãããšã§ç¶æã容æã«ãªããŸãããã ããä¿å®æ§ã容æã«ããããã«ã¯äžè²«æ§ãéèŠã§ããããšã«æ³šæããŠãã ããããããã£ãŠãã³ãŒãããŒã¹å ã§å¯èœãªéãäžè²«æ§ãä¿ã€ããã«ããŸãããã
è£è¶³
Go ããŒã 㯠Go ãããžã§ã¯ãã®çµç¹å/æ§é åã«é¢ããå ¬åŒã¬ã€ãã©ã€ã³ã 2023 幎ã«çºè¡ããŸããïŒ go.dev/doc/modules/layout
ãŠãŒãã£ãªãã£ããã±ãŒãžã®äœæ (#13)
èŠçŽ
ååä»ãã¯ã¢ããªã±ãŒã·ã§ã³èšèšã®éèŠãªéšåã§ããcommon
ãutil
ãshared
ã®ãããªããã±ãŒãžãäœæããŠããèªã¿æã«ããã»ã©ã®äŸ¡å€ããããããŸããããã®ãããªããã±ãŒãžãæå³ã®ããå
·äœçãªããã±ãŒãžåã«ãªãã¡ã¯ã¿ãªã³ã°ããŸãããã
ãŸããããã±ãŒãžã«å«ãŸãããã®ã§ã¯ãªããããã±ãŒãžãæäŸãããã®ã«åºã¥ããŠããã±ãŒãžã«ååãä»ãããšããã®è¡šçŸåãé«ããå¹ççãªæ¹æ³ã«ãªãããšã«ãçæããŠãã ããã
ããã±ãŒãžåã®è¡çªãç¡èŠãã (#14)
èŠçŽ
æ··ä¹±ãããã«ã¯ãã°ã«ã€ãªãããããªããå€æ°ãšããã±ãŒãžéã®ååã®è¡çªãåé¿ããããã«ãããããã«äžæã®ååã䜿çšããŸãããããããäžå¯èœãªå Žåã¯ãã€ã³ããŒããšã€ãªã¢ã¹ã䜿çšããŠä¿®é£Ÿåãå€æŽããŠããã±ãŒãžåãšå€æ°åãåºå¥ããããããè¯ãååãèããŠãã ããã
ããã±ãŒãžã®è¡çªã¯ãå€æ°åãæ¢åã®ããã±ãŒãžåãšè¡çªããå Žåã«çºçããããã±ãŒãžã®åå©çšã劚ããããŸããææ§ããé¿ããããã«ãå€æ°åã®è¡çªãé²ãå¿ èŠããããŸããè¡çªãçºçããå Žåã¯ãå¥ã®æå³ã®ããååãèŠã€ããããã€ã³ããŒããšã€ãªã¢ã¹ã䜿çšããå¿ èŠããããŸãã
ã³ãŒãã®æç« åãè¡ãããŠããªã (#15)
èŠçŽ
ã¯ã©ã€ã¢ã³ããšã¡ã³ãããã³ãŒãã®æå³ãç解ã§ããããã«ããšã¯ã¹ããŒããããèŠçŽ ãæç« åããŸãããã
æç« åã¯ã³ãŒãã£ã³ã°ã®éèŠãªåŽé¢ã§ããããã«ãããã¯ã©ã€ã¢ã³ãã API ãããç°¡åã«äœ¿çšããããšãã§ããŸããããããžã§ã¯ãã®ç¶æã«ã圹ç«ã¡ãŸããGoèšèªã§ã¯ãã³ãŒããæ £çšçãªãã®ã«ããããã«ãããã€ãã®ã«ãŒã«ã«åŸãå¿ èŠããããŸãã
ãŸãããšã¯ã¹ããŒãããããã¹ãŠã®èŠçŽ ãæç« åããå¿ èŠããããŸããæ§é ãã€ã³ã¿ãã§ãŒã¹ãé¢æ°ãªã©ããšã¯ã¹ããŒãããå Žåã¯æç« åããå¿ èŠããããŸããæ £äŸãšããŠããšã¯ã¹ããŒããããèŠçŽ ã®ååããå§ãŸãã³ã¡ã³ããè¿œå ããŸãã
æ £äŸãšããŠãåã³ã¡ã³ãã¯å¥èªç¹ã§çµããå®å šãªæã§ããå¿ èŠããããŸãããŸããé¢æ°ïŒãŸãã¯ã¡ãœããïŒãæç« åãããšãã¯ãé¢æ°ãã©ã®ããã«å®è¡ãããã§ã¯ãªãããã®é¢æ°ãäœãå®è¡ããã€ããã§ãããã匷調ããå¿ èŠãããããšã«ãçæããŠãã ãããããã¯ããã¥ã¡ã³ãã§ã¯ãªããé¢æ°ãšã³ã¡ã³ãã«ã€ããŠã§ããããã¥ã¡ã³ãã¯çæ³çã«ã¯ãå©çšè ããšã¯ã¹ããŒããããèŠçŽ ã®äœ¿çšæ¹æ³ãç解ããããã«ã³ãŒããèŠãå¿ èŠããªãã»ã©ååãªæ å ±ãæäŸããå¿ èŠããããŸãã
å€æ°ãŸãã¯å®æ°ãæç« åããå Žåããã®ç®çãšå 容ãšãã 2 ã€ã®åŽé¢ãäŒããããšãéèŠãããããŸãããåè ã¯ãå€éšã¯ã©ã€ã¢ã³ãã«ãšã£ãŠåœ¹ç«ã€ããã«ãã³ãŒãããã¥ã¡ã³ããšããŠååšããå¿ èŠããããŸãããã ããåŸè ã¯å¿ ãããå ¬éãããã¹ãã§ã¯ãããŸããã
ã¯ã©ã€ã¢ã³ããšã¡ã³ãããããã±ãŒãžã®ç®çãç解ã§ããããã«ãåããã±ãŒãžãããã¥ã¡ã³ãããå¿
èŠããããŸããæ
£äŸãšããŠãã³ã¡ã³ã㯠//Package
ã§å§ãŸãããã®åŸã«ããã±ãŒãžåãç¶ããŸããããã±ãŒãžã³ã¡ã³ãã®æåã®è¡ã¯ãããã±ãŒãžã«è¡šç€ºãããããç°¡æœã«ããå¿
èŠããããŸãããããŠã次ã®è¡ã«å¿
èŠãªæ
å ±ããã¹ãŠå
¥åããŸãã
ã³ãŒããæç« åããããšãå¶çŽã«ãªãã¹ãã§ã¯ãããŸãããã¯ã©ã€ã¢ã³ããã¡ã³ãããã³ãŒãã®æå³ãç解ããã®ã«åœ¹ç«ã€å¿ èŠããããŸãã
ãªã³ã¿ãŒã䜿çšããŠãªã (#16)
èŠçŽ
ã³ãŒãã®å質ãšäžè²«æ§ãåäžãããã«ã¯ããªã³ã¿ãŒãšãã©ãŒããã¿ãŒã䜿çšããŸããã
ãªã³ã¿ãŒã¯ãã³ãŒããåæããŠãšã©ãŒãæ€åºããèªåããŒã«ã§ãããã®ã»ã¯ã·ã§ã³ã®ç®çã¯ãæ¢åã®ãªã³ã¿ãŒã®å®å šãªãªã¹ããæäŸããããšã§ã¯ãããŸãããããããå Žåãããã«äœ¿ãç©ã«ãªããªããªã£ãŠããŸãããã§ãããã ããã»ãšãã©ã® Go ãããžã§ã¯ãã«ãªã³ã¿ãŒãäžå¯æ¬ ã§ãããšããããšã¯ç解ããèŠããŠãããŸãããã
- https://golang.org/cmd/vetââGoèšèªã®æšæºã³ãŒãã¢ãã©ã€ã¶ãŒ
- https://github.com/kisielk/errcheckââãšã©ãŒãã§ãã«ãŒ
- https://github.com/fzipp/gocycloââ埪ç°çè€é床ã¢ãã©ã€ã¶ãŒ
- https://github.com/jgautheron/goconstââè€æ°å䜿çšæååã¢ãã©ã€ã¶ãŒ
ãªã³ã¿ãŒã®ã»ãã«ãã³ãŒãã¹ã¿ã€ã«ãä¿®æ£ããããã«ã³ãŒããã©ãŒããã¿ãŒã䜿çšããŸãããã以äžã«ãããã€ãã®ã³ãŒããã©ãŒããã¿ãŒã瀺ããŸãã
- https://golang.org/cmd/gofmtââGoèšèªã®æšæºã³ãŒããã©ãŒããã¿ãŒ
- https://godoc.org/golang.org/x/tools/cmd/goimportsââGoèšèªã®æšæºã€ã³ããŒããã©ãŒããã¿ãŒ
ã»ãã« golangci-lint (https://github.com/golangci/golangci-lint) ãšãããã®ããããŸããããã¯ãå€ãã®äŸ¿å©ãªãªã³ã¿ãŒããã©ãŒããã¿ãŒã®äžã«ãã¡ãµãŒããæäŸãããªã³ãã£ã³ã°ããŒã«ã§ãããŸãããªã³ã¿ãŒã䞊åå®è¡ããŠåæé床ãåäžãããããšãã§ããéåžžã«äŸ¿å©ã§ãã
ãªã³ã¿ãŒãšãã©ãŒããã¿ãŒã¯ãã³ãŒãããŒã¹ã®å質ãšäžè²«æ§ãåäžããã匷åãªæ¹æ³ã§ããæéããããŠã©ãã䜿çšãã¹ãããç解ãããããã®å®è¡ïŒ CI ã Git ããªã³ãããããã¯ãªã©ïŒãèªååããŸãããã
ããŒã¿å
8 é²æ°ãªãã©ã«ã§æ··ä¹±ãæããŠããŸã (#17)
èŠçŽ
æ¢åã®ã³ãŒããèªããšãã¯ã 0
ã§å§ãŸãæŽæ°ãªãã©ã«ã 8 é²æ°ã§ããããšã«çæããŠãã ããããŸããæ¥é èŸ 0o
ãä»ããããšã§8é²æ°ã§ããããšãæ確ã«ããèªã¿ããããåäžãããŸãããã
8 é²æ°ã¯ 0 ã§å§ãŸããŸãïŒããšãã°ã010
㯠10 é²æ°ã® 8 ã«çžåœããŸãïŒãå¯èªæ§ãåäžãããå°æ¥ã®ã³ãŒããªãŒããŒã®æœåšçãªééããåé¿ããã«ã¯ã 0o
æ¥é èŸã䜿çšã㊠8 é²æ°ã§ããããšãæããã«ããŸãããïŒäŸ: 0o10
ïŒã
ä»ã®æŽæ°ãªãã©ã«è¡šçŸã«ã泚æããŠãã ããã
- ãã€ã㪠- æ¥é èŸ
0b
ãããã¯0B
ã䜿çšããŸãïŒããšãã°ã0b
㯠10 é²æ°ã® 4 ã«çžåœããŸãïŒ - 16é²æ° - æ¥é èŸ
0x
ãããã¯0X
ã䜿çšããŸãïŒããšãã°ã0xF
㯠10 é²æ°ã® 15 ã«çžåœããŸãïŒã - èæ° - æ¥å°ŸèŸ
i
ã䜿çšããŸãïŒããšãã°ã3i
ïŒ
èªã¿ãããããããã«ãåºåãæåãšããŠã¢ã³ããŒã¹ã³ã¢ïŒ _ ïŒã䜿çšããããšãã§ããŸããããšãã°ã 10 å㯠1_000_000_000
ã®ããã«æžãããšãã§ããŸããã¢ã³ããŒã¹ã³ã¢ã¯ 0b)00_00_01
ã®ããã«ä»ã®è¡šçŸãšäœµçšããããšãã§ããŸãã
æŽæ°ãªãŒããŒãããŒãç¡èŠããŠãã (#18)
èŠçŽ
Goèšèªã§ã¯æŽæ°ã®ãªãŒããŒãããŒãšã¢ã³ããŒãããŒãè£åŽã§åŠçãããããããããããã£ããããç¬èªã®é¢æ°ãå®è£ ã§ããŸãã
Goèšèªã§ã¯ãã³ã³ãã€ã«æã«æ€åºã§ããæŽæ°ãªãŒããŒãããŒã«ãã£ãŠã³ã³ãã€ã«ãšã©ãŒãçæãããŸããããšãã°ã次ã®ããã«ãªããŸãã
ãã ããå®è¡æã«ã¯ãæŽæ°ã®ãªãŒããŒãããŒãŸãã¯ã¢ã³ããŒãããŒã¯çºçããŸãããããã«ãã£ãŠã¢ããªã±ãŒã·ã§ã³ã®ãããã¯ãçºçããããšã¯ãããŸããããã®åäœã¯ãã£ãããªãã°ïŒããšãã°ãè² ã®çµæã«ã€ãªããæŽæ°ã®å¢åãæ£ã®æŽæ°ã®å ç®ãªã©ïŒã«ã€ãªããå¯èœæ§ããããããé ã«å ¥ããŠããããšãéèŠã§ãã
æµ®åå°æ°ç¹ãç解ããŠããªã (#19)
èŠçŽ
ç¹å®ã®ãã«ã¿å ã§æµ®åå°æ°ç¹æ¯èŒãè¡ããšãã³ãŒãã®ç§»æ€æ§ã確ä¿ã§ããŸããå ç®ãŸãã¯æžç®ãå®è¡ãããšãã¯ã粟床ãåäžãããããã«ãåçšåºŠã®å€§ããã®æŒç®ãã°ã«ãŒãåããŠãã ããããŸããä¹ç®ãšé€ç®ã¯å ç®ãšæžç®ã®åã«å®è¡ããŠãã ããã
Goèšèªã«ã¯ãïŒèæ°ãé€ããå ŽåïŒ float32
ãš float64
ãšãã 2 ã€ã®æµ®åå°æ°ç¹åããããŸããæµ®åå°æ°ç¹ã®æŠå¿µã¯ãå°æ°å€ãè¡šçŸã§ããªããšããæŽæ°ã®å€§ããªåé¡ã解決ããããã«çºæãããŸãããäºæ³å€ã®äºæ
ãé¿ããããã«ãæµ®åå°æ°ç¹æŒç®ã¯å®éã®æŒç®ã®è¿äŒŒã§ããããšãç¥ã£ãŠããå¿
èŠããããŸãã
ãã®ããã«ãä¹ç®ã®äŸãèŠãŠã¿ãŸãããã
ãã®ã³ãŒãã«ãããŠã¯ 1.0001 * 1.0001 = 1.00020001 ãšããçµæãåºåãããããšãæåŸ ãããšæããŸããããããªãããã»ãšãã©ã® x86 ããã»ããµã§ã¯ã代ããã« 1.0002 ãåºåãããŸãã
Goèšèªã® float32
ããã³ float64
åã¯è¿äŒŒå€ã§ãããããããã€ãã®ã«ãŒã«ã念é ã«çœ®ãå¿
èŠããããŸãã
- 2 ã€ã®æµ®åå°æ°ç¹æ°ãæ¯èŒããå Žåã¯ããã®å·®ã蚱容ç¯å²å ã§ããããšã確èªããã
- å ç®ãŸãã¯æžç®ãå®è¡ããå Žåã粟床ãé«ããããã«ãåãæ¡æ°ã®æŒç®ãã°ã«ãŒãåããã
- 粟床ãé«ãããããäžé£ã®æŒç®ã§å ç®ãæžç®ãä¹ç®ãé€ç®ãå¿ èŠãªå Žåã¯ãä¹ç®ãšé€ç®ãæåã«å®è¡ããã
ã¹ã©ã€ã¹ã®é·ããšå®¹éãç解ããŠããªã (#20)
èŠçŽ
Go éçºè ãªãã°ãã¹ã©ã€ã¹ã®é·ããšå®¹éã®éããç解ããã¹ãã§ããã¹ã©ã€ã¹ã®é·ãã¯ã¹ã©ã€ã¹å ã®äœ¿çšå¯èœãªèŠçŽ ã®æ°ã§ãããã¹ã©ã€ã¹ã®å®¹éã¯ãããã³ã°é åå ã®èŠçŽ ã®æ°ã§ãã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
éå¹çãªã¹ã©ã€ã¹ã®åæå (#21)
èŠçŽ
ã¹ã©ã€ã¹ãäœæãããšããé·ãããã§ã«ããã£ãŠããå Žåã¯ãæå®ãããé·ããŸãã¯å®¹éã§ã¹ã©ã€ã¹ãåæåããŸããããããã«ãããå²ãåœãŠã®æ°ãæžããããã©ãŒãã³ã¹ãåäžããŸãã
make
ã䜿çšããŠã¹ã©ã€ã¹ãåæåãããšãã«ãé·ããšãªãã·ã§ã³ã®å®¹éãæå®ã§ããŸãããããã®ãã©ã¡ãŒã¿ã®äž¡æ¹ã«é©åãªå€ãæž¡ãããšãé©åœã§ããã«ããããããããããå¿ããã®ã¯ããããééãã§ããå®éãè€æ°ã®ã³ããŒãå¿
èŠã«ãªããäžæçãªãããã³ã°é
åãã¯ãªãŒã³ã¢ããããããã« GC ã«è¿œå ã®åŽåããããå¯èœæ§ããããŸããããã©ãŒãã³ã¹ã®èŠ³ç¹ããèšãã°ãGo ã©ã³ã¿ã€ã ã«æãå·®ã䌞ã¹ãªãçç±ã¯ãããŸããã
ãªãã·ã§ã³ã¯ãæå®ããã容éãŸãã¯æå®ãããé·ãã®ã¹ã©ã€ã¹ãå²ãåœãŠãããšã§ãã ããã 2 ã€ã®è§£æ±ºçã®ãã¡ã2 çªç®ã®è§£æ±ºçã®æ¹ããããã«é«éã§ããåŸåãããããšãããããŸããããã ããç¹å®ã®å®¹éãšè¿œå ã䜿çšãããšãå Žåã«ãã£ãŠã¯å®è£ ãšèªã¿åãã容æã«ãªãããšããããŸãã
nil ãšç©ºã®ã¹ã©ã€ã¹ãæ··åããŠãã (#22)
èŠçŽ
encoding/json
ã reflect
ããã±ãŒãžãªã©ã䜿çšãããšãã«ããããæ··ä¹±ãé¿ããããã«ã¯ãnil ã¹ã©ã€ã¹ãšç©ºã®ã¹ã©ã€ã¹ã®éããç解ããå¿
èŠããããŸããã©ã¡ããé·ããŒãã容éãŒãã®ã¹ã©ã€ã¹ã§ãããå²ãåœãŠãå¿
èŠãšããªãã®ã¯ nil ã¹ã©ã€ã¹ã ãã§ãã
Goèšèªã§ã¯ãnil ãšç©ºã®ã¹ã©ã€ã¹ã¯åºå¥ãããŸããnil ã¹ã©ã€ã¹ã¯ nil
ã«çããã®ã«å¯Ÿãã空ã®ã¹ã©ã€ã¹ã®é·ãã¯ãŒãã§ããnil ã¹ã©ã€ã¹ã¯ç©ºã§ããã空ã®ã¹ã©ã€ã¹ã¯å¿
ãããnil
ã§ãããšã¯éããŸãããäžæ¹ãnil ã¹ã©ã€ã¹ã«ã¯å²ãåœãŠã¯å¿
èŠãããŸããããã®ã»ã¯ã·ã§ã³å
šäœãéããŠã以äžã®æ¹æ³ã䜿çšããããšã«ãã£ãŠãç¶æ³ã«å¿ããŠã¹ã©ã€ã¹ãåæåããããšãèŠãŠããŸããã
- æçµçãªé·ããäžæã§ã¹ã©ã€ã¹ã空ã®å Žåã¯
var s []string
- nil ãšç©ºã®ã¹ã©ã€ã¹ãäœæããç³è¡£æ§æãšããŠã®
[]string(nil)
- å°æ¥ã®é·ããããã£ãŠããå Žåã¯
make([]string, length)
èŠçŽ ãªãã§ã¹ã©ã€ã¹ãåæåããå ŽåãæåŸã®ãªãã·ã§ã³ []string{}
ã¯é¿ããã¹ãã§ããæåŸã«ãäºæ³å€ã®åäœãé²ãããã«ã䜿çšããã©ã€ãã©ãªã nil ãšç©ºã®ã¹ã©ã€ã¹ãåºå¥ããŠãããã©ããã確èªããŠã¿ãŸãããã
ã¹ã©ã€ã¹ã空ãã©ãããé©åã«ç¢ºèªããªã (#23)
èŠçŽ
ã¹ã©ã€ã¹ã«èŠçŽ ãå«ãŸããŠããªãããšã確èªããã«ã¯ããã®é·ãã確èªããŸããããããã¯ãã¹ã©ã€ã¹ã nil
ã§ããã空ã§ãããã«é¢ä¿ãªãæ©èœããŸãããããã«ã€ããŠãåæ§ã§ããæ確㪠API ãèšèšããã«ã¯ãnil ã¹ã©ã€ã¹ãšç©ºã®ã¹ã©ã€ã¹ãåºå¥ããªãã§ãã ããã
ã¹ã©ã€ã¹ã«èŠçŽ ããããã©ãããå€æããã«ã¯ãã¹ã©ã€ã¹ã nil ãã©ããããŸãã¯ãã®é·ãã 0 ã«çãããã©ããã確èªããããšã§å€æã§ããŸããã¹ã©ã€ã¹ã空ã§ããå Žåãšã¹ã©ã€ã¹ã nil ã§ããå Žåã®äž¡æ¹ãã«ããŒã§ãããããé·ãã確ãããããšãæè¯ã®æ¹æ³ã§ãã
äžæ¹ãã€ã³ã¿ãã§ãŒã¹ãèšèšãããšãã¯ã軜埮ãªããã°ã©ãã³ã°ãšã©ãŒãèµ·ãããªããã nil ã¹ã©ã€ã¹ãšç©ºã®ã¹ã©ã€ã¹ãåºå¥ããªãããã«ããå¿ èŠããããŸããã¹ã©ã€ã¹ãè¿ããšãã«ãnil ãŸãã¯ç©ºã®ã¹ã©ã€ã¹ãè¿ããã©ããã¯ãæå³çã«ãæè¡çã«ãéãã¯ãããŸãããã³ãŒã©ãŒã«ãšã£ãŠã¯ã©ã¡ããåãããšãæå³ããã¯ãã§ãããã®åçã¯ãããã§ãåãã§ãããããã空ãã©ããã確èªããã«ã¯ãããã nil ãã©ããã§ã¯ãªãããã®é·ãã確èªããŸãããã
ã¹ã©ã€ã¹ã®ã³ããŒãæ£ããäœæããŠããªã (#24)
èŠçŽ
çµã¿èŸŒã¿é¢æ° copy
ã䜿çšããŠããã¹ã©ã€ã¹ãå¥ã®ã¹ã©ã€ã¹ã«ã³ããŒããã«ã¯ãã³ããŒãããèŠçŽ ã®æ°ã 2 ã€ã®ã¹ã©ã€ã¹ã®é·ãã®éã®æå°å€ã«çžåœããããšã«æ³šæããŠãã ããã
èŠçŽ ãããã¹ã©ã€ã¹ããå¥ã®ã¹ã©ã€ã¹ã«ã³ããŒããæäœã¯ãããªãé »ç¹ã«è¡ãããŸããã³ããŒã䜿çšããå Žåãã³ããŒå ã«ã³ããŒãããèŠçŽ ã®æ°ã¯ 2 ã€ã®ã¹ã©ã€ã¹ã®é·ãã®éã®æå°å€ã«çžåœããããšã«æ³šæããå¿ èŠããããŸãããŸããã¹ã©ã€ã¹ãã³ããŒããããã®ä»ã®ä»£æ¿æ段ãååšããããšã«ãçæããŠãã ããããã®ãããã³ãŒãããŒã¹ã§ããããèŠã€ããŠãé©ãããšã¯ãããŸããã
append
ã®äœ¿çšã«ããäºæ³å€ã®å¯äœçš (#25)
èŠçŽ
2ã€ã®ç°ãªãé¢æ°ãåãé
åã«åºã¥ãã¹ã©ã€ã¹ã䜿çšããå Žåã«ãcopy ãŸãã¯å®å
šã¹ã©ã€ã¹åŒã䜿çšããããšã§ append
ã«ããè¡çªãé²ãããšãã§ããŸãããã ãã倧ããªã¹ã©ã€ã¹ãçž®å°ããå Žåãã¡ã¢ãªãªãŒã¯ãé²ãããšãã§ããã®ã¯ã¹ã©ã€ã¹ã®ã³ããŒã ãã§ãã
ã¹ã©ã€ã¹ã䜿çšãããšãã¯ãäºæ³å€ã®å¯äœçšã«ã€ãªããç¶æ³ã«çŽé¢ããå¯èœæ§ãããããšãèŠããŠããå¿ èŠããããŸããçµæã®ã¹ã©ã€ã¹ã®é·ãããã®å®¹éããå°ããå Žåãè¿œå ã«ãã£ãŠå ã®ã¹ã©ã€ã¹ãå€æŽãããå¯èœæ§ããããŸããèµ·ããåŸãå¯äœçšã®ç¯å²ãå¶éãããå Žåã¯ãã¹ã©ã€ã¹ã®ã³ããŒãŸãã¯å®å šã¹ã©ã€ã¹åŒã䜿çšã§ããŸããããã«ãããã³ããŒãå®è¡ã§ããªããªããŸãã
è£è¶³
s[low:high:max]
ïŒå®å
šã¹ã©ã€ã¹åŒïŒââãã®åœä»€æã¯ã容éã max - low
ã«çããããšãé€ãã°ãs[low:high]
ã§äœæãããã¹ã©ã€ã¹ãšåæ§ã®ã¹ã©ã€ã¹ãäœæããŸãã
ã¹ã©ã€ã¹ãšã¡ã¢ãªãªãŒã¯ (#26)
èŠçŽ
ãã€ã³ã¿ã®ã¹ã©ã€ã¹ãŸãã¯ãã€ã³ã¿ãã£ãŒã«ããæã€æ§é äœãæäœããå Žåãã¹ã©ã€ã¹æäœã«ãã£ãŠé€å€ãããèŠçŽ ã nil ãšããããšã§ã¡ã¢ãªãªãŒã¯ãåé¿ã§ããŸãã
容éæŒã
倧ããªã¹ã©ã€ã¹ãŸãã¯é åãã¹ã©ã€ã¹ãããšãã¡ã¢ãªæ¶è²»ãé«ããªãå¯èœæ§ãããããšã«æ³šæããŠãã ãããæ®ãã®ã¹ããŒã¹ã¯ GC ã«ãã£ãŠåå©çšããããå°æ°ã®èŠçŽ ãã䜿çšããªãã«ããããããã倧ããªãããã³ã°é åãä¿æãããŸããã¹ã©ã€ã¹ã®ã³ããŒãããããšã§ããã®ãããªäºæ ãé²ãããšãã§ããŸãã
ã¹ã©ã€ã¹ãšãã€ã³ã¿
ãã€ã³ã¿ãŸãã¯ãã€ã³ã¿ãã£ãŒã«ããå«ãæ§é äœã䜿çšããŠã¹ã©ã€ã¹æäœãããå ŽåãGC ããããã®èŠçŽ ãåå©çšããªãããšãç¥ã£ãŠããå¿
èŠããããŸãããã®å Žåã®éžæè¢ã¯ãã³ããŒãå®è¡ããããæ®ãã®èŠçŽ ãŸãã¯ãã®ãã£ãŒã«ããæ瀺çã« nil
ãšããããšã§ãã
éå¹çãªãããã®åæå (#27)
èŠçŽ
ããããäœæãããšãããã®é·ãããã§ã«ããã£ãŠããå Žåã¯ãæå®ãããé·ãã§åæåããŸããããã«ãããå²ãåœãŠã®æ°ãæžããããã©ãŒãã³ã¹ãåäžããŸãã
ãããã¯ãããŒã»å€ãã¢ã®é åºãªãã³ã¬ã¯ã·ã§ã³ãæäŸããŸãããªããããããã®ãã¢ã¯åºæã®ããŒãæã¡ãŸããGoèšèªã§ã¯ããããã¯ããã·ã¥ããŒãã«ããŒã¿æ§é ã«åºã¥ããŠããŸããå éšçã«ã¯ãããã·ã¥ããŒãã«ã¯ãã±ããã®é åã§ãããåãã±ããã¯ããŒã»å€ãã¢ã®é åãžã®ãã€ã³ã¿ã§ãã
ãããã«å«ãŸããèŠçŽ ã®æ°ãäºåã«ããã£ãŠããå Žåã¯ããã®åæãµã€ãºãæå®ããŠäœæããå¿ èŠããããŸãããããã®å¢å€§ã¯ãååãªã¹ããŒã¹ãåå²ãåœãŠãããã¹ãŠã®èŠçŽ ã®ãã©ã³ã¹ãå調æŽããå¿ èŠããããããèšç®éãéåžžã«å€ããªããŸãããããã«ãããããåé¿ããããšãã§ããŸãã
ããããšã¡ã¢ãªãªãŒã¯ (#28)
èŠçŽ
ãããã¯ã¡ã¢ãªå ã§åžžã«å¢å€§ããå¯èœæ§ããããŸãããçž®å°ããããšã¯ãããŸããããããã£ãŠãã¡ã¢ãªã®åé¡ãçºçããå Žåã¯ããããã匷å¶çã«åçæãããããã€ã³ã¿ã䜿çšããããããªã©ãããŸããŸãªæ段ãè©Šãããšãã§ããŸãã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
誀ã£ãæ¹æ³ã«ããå€ã®æ¯èŒ (#29)
èŠçŽ
Goèšèªã§åãæ¯èŒãââãã«ã¯ã2 ã€ã®åãæ¯èŒå¯èœãªãã°ã== æŒç®åãš != æŒç®åã䜿çšã§ããŸããçåœå€ãæ°å€ãæååããã€ã³ã¿ããã£ãã«ãããã³æ§é äœãå®å
šã«æ¯èŒå¯èœãªåã§æ§æãããŠããŸãããã以å€ã¯ã reflect.DeepEqual
ã䜿çšããŠãªãã¬ã¯ã·ã§ã³ã®ä»£åãæ¯æãããç¬èªã®å®è£
ãšã©ã€ãã©ãªã䜿çšããããšãã§ããŸãã
å¹æçã«æ¯èŒããã«ã¯ã ==
ãš !=
ã®äœ¿çšæ¹æ³ãç解ããããšãäžå¯æ¬ ã§ãããããã®æŒç®åã¯ãæ¯èŒå¯èœãªè¢«æŒç®åã§äœ¿çšã§ããŸãã
- çåœå€ - 2 ã€ã®çåœå€ãçãããã©ãããæ¯èŒããŸãã
- æ°å€ (intãfloatãããã³ complex å) - 2 ã€ã®æ°å€ãçãããã©ãããæ¯èŒããŸãã
- æåå - 2 ã€ã®æååãçãããã©ãããæ¯èŒããŸãã
- ãã£ãã« - 2 ã€ã®ãã£ãã«ãåã make åŒã³åºãã«ãã£ãŠäœæããããããŸãã¯äž¡æ¹ã nil ã§ããããæ¯èŒããŸãã
- ã€ã³ã¿ãã§ãŒã¹ - 2 ã€ã®ã€ã³ã¿ãã§ãŒã¹ãåãåçã¿ã€ããšçããåçå€ãæã€ãã©ããããŸãã¯äž¡æ¹ã nil ã§ãããã©ãããæ¯èŒããŸãã
- ãã€ã³ã¿ - 2 ã€ã®ãã€ã³ã¿ãã¡ã¢ãªå ã®åãå€ãæããŠãããããŸãã¯äž¡æ¹ãšã nil ã§ããããæ¯èŒããŸãã
- æ§é äœãšé å - é¡äŒŒããåã§æ§æãããŠãããã©ãããæ¯èŒããŸãã
è£è¶³
?
ã >=
ã <
ãããã³ >
æŒç®åãæ°å€åã§äœ¿çšããŠå€ãæ¯èŒããããæååã§åå¥é åºãæ¯èŒãããããããšãã§ããŸãã
被æŒç®åãæ¯èŒã§ããªãå ŽåïŒã¹ã©ã€ã¹ãšããããªã©ïŒããªãã¬ã¯ã·ã§ã³ãªã©ã®ä»ã®æ¹æ³ãå©çšããå¿
èŠããããŸãããªãã¬ã¯ã·ã§ã³ã¯ã¡ã¿ããã°ã©ãã³ã°ã®äžçš®ã§ãããã¢ããªã±ãŒã·ã§ã³ããã®æ§é ãšåäœãå
çããŠå€æŽããæ©èœãæããŸããããšãã°ãGoèšèªã§ã¯ reflect.DeepEqual
ã䜿çšã§ããŸãããã®é¢æ°ã¯ã2ã€ã®å€ãååž°çã«èª¿ã¹ãããšã«ãã£ãŠã2ã€ã®èŠçŽ ãå®å
šã«çãããã©ãããå ±åããŸããåãå
¥ããããèŠçŽ ã¯ãåºæ¬åã«å ããŠãé
åãæ§é äœãã¹ã©ã€ã¹ããããããã€ã³ã¿ãã€ã³ã¿ãã§ãŒã¹ãé¢æ°ã§ããããããæ倧ã®èœãšãç©Žã¯ããã©ãŒãã³ã¹äžã®ããã«ãã£ã§ãã
å®è¡æã®ããã©ãŒãã³ã¹ãéèŠãªå Žåã¯ãç¬èªã®ã¡ãœãããå®è£ ããããšãæåãšãªãå¯èœæ§ããããŸãã
è¿œèšïŒæšæºã©ã€ãã©ãªã«ã¯æ¢ã«æ¯èŒã¡ãœãããããã€ãããããšãèŠããŠããå¿
èŠããããŸããããšãã°ãæé©åããã bytes.Compare
é¢æ°ã䜿çšããŠã2ã€ã®ãã€ãã¹ã©ã€ã¹ãæ¯èŒã§ããŸããç¬èªã®ã¡ãœãããå®è£
ããåã«ãè»èŒªã®åçºæãããªãããã«ããŸãããã
å¶åŸ¡æ§é
èŠçŽ ã range
ã«ãŒãå
ã§ã³ããŒãããããšãç¥ããªã (#30)
èŠçŽ
range
ã«ãŒãå
ã® value èŠçŽ ã¯ã³ããŒã§ãããããã£ãŠãããšãã°æ§é äœãå€æŽããã«ã¯ããã®ã€ã³ããã¯ã¹ãä»ããŠã¢ã¯ã»ã¹ããããåŸæ¥ã® for
ã«ãŒããä»ããŠã¢ã¯ã»ã¹ããŸãããïŒå€æŽããèŠçŽ ãŸãã¯ãã£ãŒã«ãããã€ã³ã¿ã§ããå Žåãé€ãïŒã
range ã«ãŒãã䜿çšãããšãããŸããŸãªããŒã¿æ§é ã«å埩åŠçãè¡ãããšãã§ããŸãã
- æåå
- é å
- é åãžã®ãã€ã³ã¿
- ã¹ã©ã€ã¹
- ããã
- åä¿¡ãã£ãã«
å€å
žç㪠for
ã«ãŒããšæ¯èŒãããšãrange
ã«ãŒãã¯ãã®ç°¡æœãªæ§æã®ãããã§ããããã®ããŒã¿æ§é ã®ãã¹ãŠã®èŠçŽ ã«å埩åŠçãããã®ã«äŸ¿å©ã§ãã
ãã ããrange ã«ãŒãå ã®å€èŠçŽ ã¯ã³ããŒã§ããããšãèŠããŠããå¿ èŠããããŸãããããã£ãŠãå€ãå€æŽããå¿ èŠãããæ§é äœã®å Žåãå€æŽããå€ãŸãã¯ãã£ãŒã«ãããã€ã³ã¿ã§ãªãéããèŠçŽ èªäœã§ã¯ãªãã³ããŒã®ã¿ãæŽæ°ããŸããrange ã«ãŒããŸãã¯åŸæ¥ã® for ã«ãŒãã䜿çšããŠã€ã³ããã¯ã¹çµç±ã§èŠçŽ ã«ã¢ã¯ã»ã¹ããããšãæšå¥šãããŸãã
range
ã«ãŒãïŒãã£ãã«ãšé
åïŒã§ã®åŒæ°ã®è©äŸ¡æ¹æ³ãç¥ããªã (#31)
èŠçŽ
range
æŒç®åã«æž¡ãããåŒã¯ã«ãŒãã®éå§åã« 1 åã ãè©äŸ¡ãããããšãç解ãããšããã£ãã«ãŸãã¯ã¹ã©ã€ã¹ã®å埩åŠçã«ãããéå¹çãªå²ãåœãŠãªã©ã®ãããã¡ãªééããåé¿ã§ããŸãã
range ã«ãŒãã¯ãïŒã¿ã€ãã«é¢ä¿ãªãïŒã³ããŒãå®è¡ããããšã«ãããã«ãŒãã®éå§åã«ãæå®ãããåŒã 1 åã ãè©äŸ¡ããŸããããšãã°ã誀ã£ãèŠçŽ ã«ã¢ã¯ã»ã¹ããŠããŸãããšãããããªãããã¡ãªééããé¿ããããã«ããã®åäœãèŠããŠããå¿ èŠããããŸããããšãã°
ãã®ã³ãŒãã¯ãæåŸã®ã€ã³ããã¯ã¹ã 10 ã«æŽæ°ããŸãããããããã®ã³ãŒããå®è¡ãããšã10 ã¯åºåãããŸããã 2 ãåºåãããŸãã
range
ã«ãŒãå
ã«ããããã€ã³ã¿èŠçŽ ã®äœ¿çšãåãŒã圱é¿ãåãã£ãŠããªã (#32)
èŠçŽ
ããŒã«ã«å€æ°ã䜿çšããããã€ã³ããã¯ã¹ã䜿çšããŠèŠçŽ ã«ã¢ã¯ã»ã¹ãããšãã«ãŒãå ã§ãã€ã³ã¿ãã³ããŒããéã®ééããé²ãããšãã§ããŸãã
range
ã«ãŒãã䜿çšããŠããŒã¿æ§é ã«å埩åŠçãæœãå Žåããã¹ãŠã®å€ãåäžã®äžæã®ã¢ãã¬ã¹ãæã€äžæã®å€æ°ã«å²ãåœãŠãããããšãæãåºããŠãã ãããããã«ãåå埩åŠçäžã«ãã®å€æ°ãåç
§ãããã€ã³ã¿ãä¿åãããšãåãèŠçŽ ãã€ãŸãææ°ã®èŠçŽ ãåç
§ããåããã€ã³ã¿ãä¿åããããšã«ãªããŸãããã®åé¡ã¯ãã«ãŒãã®ã¹ã³ãŒãå
ã«ããŒã«ã«å€æ°ã匷å¶çã«äœæããããã€ã³ããã¯ã¹ãä»ããŠã¹ã©ã€ã¹èŠçŽ ãåç
§ãããã€ã³ã¿ãäœæããããšã§è§£æ±ºã§ããŸããã©ã¡ãã®è§£æ±ºçã§ãåé¡ãããŸããã
ãããã®å埩åŠçäžã«èª€ã£ãä»®å®ãããïŒå埩åŠçäžã®é åºä»ããšãããã®æ¿å ¥ïŒ (#33)
èŠçŽ
ãããã䜿çšãããšãã«äºæž¬å¯èœãªåºåãä¿èšŒããã«ã¯ããããã®ããŒã¿æ§é ã次ã®ãšããã§ããããšã«æ³šæããŠãã ããã
- ããŒã¿ãããŒã§äžŠã¹æ¿ããŸãã
- æ¿å ¥é åºã¯ä¿æãããŸãã
- å埩åŠçé åºã¯æ±ºãŸã£ãŠããŸãã
- ããå埩åŠçäžã«è¿œå ãããèŠçŽ ããã®åŠçäžã«çæãããããšãä¿èšŒããŸãã
break
æãã©ã®ããã«æ©èœããããåãã£ãŠããªã (#34)
èŠçŽ
ã©ãã«ãš break
ãŸã㯠continue
ã®äœµçšã¯ãç¹å®ã®åœä»€æã匷å¶çã«äžæããŸããããã¯ãã«ãŒãå
ã® switch
ãŸã㯠select
æã§åœ¹ç«ã¡ãŸãã
éåžžãbreak æã¯ã«ãŒãã®å®è¡ãçµäºããããã«äœ¿çšãããŸããã«ãŒãã switch ãŸã㯠select ãšçµã¿åãããŠäœ¿çšââãããå Žåãç®çã®åœä»€æã§ã¯ãªãã®ã«äžæãããŠããŸãããšãããã¹ãããããšãéçºè ã«ã¯ãããããŸããããšãã°
break æ㯠for
ã«ãŒããçµäºãããã®ã§ã¯ãªãã代ããã« switch
æãçµäºãããŸãããããã£ãŠããã®ã³ãŒã㯠0 ãã 2 ãŸã§ãå埩ãã代ããã«ã0 ãã 4 ãŸã§ãå埩ããŸãïŒ0 1 2 3 4
ïŒã
èŠããŠããã¹ãéèŠãªã«ãŒã«ã®1ã€ã¯ã break
æã¯æãå
åŽã® for
ãswitch
ããŸã㯠select
æã®å®è¡ãçµäºãããšããããšã§ããåã®äŸã§ã¯ãswitch
æãçµäºããŸãã
switch
æã®ä»£ããã«ã«ãŒããäžæããæãæ
£çšçãªæ¹æ³ã¯ã©ãã«ã䜿çšããããšã§ãã
ããã§ã¯ãloop
ã©ãã«ã for
ã«ãŒãã«é¢é£ä»ããŸãã 次ã«ãbreak
æã« loop
ã©ãã«ãæå®ããã®ã§ãswitch ã§ã¯ãªã loop ãäžæãããŸãããã£ãŠããã®æ°ããããŒãžã§ã³ã¯äºæ³ã©ãã 0 1 2
ãåºåããŸãã
ã«ãŒãå
㧠defer
ã䜿çšãã (#35)
èŠçŽ
é¢æ°å
ã®ã«ãŒãããžãã¯ã®æœåºã¯ãåå埩ã®æåŸã§ã® defer
æã®å®è¡ã«ã€ãªãããŸãã
defer
æã¯ãäžäœãããã¯ã®é¢æ°ãæ»ããŸã§åŒã³åºãã®å®è¡ãé
ãããŸããããã¯äž»ã«å®åã³ãŒããåæžããããã«äœ¿çšãããŸããããšãã°ããªãœãŒã¹ãæçµçã«éããå¿
èŠãããå Žåã¯ãdefer
ã䜿çšããŠãreturn
ãå®è¡ããåã«ã¯ããŒãžã£åŒã³åºããç¹°ãè¿ãããšãé¿ããããšãã§ããŸãã
defer
ã§ãããããã¹ã®1ã€ã¯ãäžäœããã㯠ã®é¢æ°ãæ»ã£ããšãã«é¢æ°åŒã³åºããã¹ã±ãžã¥ãŒã«ãããããšãå¿ããããšã§ããããšãã°
func readFiles(ch <-chan string) error {
for path := range ch {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// ãã¡ã€ã«ã®åŠçããã
}
return nil
}
defer
åŒã³åºãã¯ãåã«ãŒãå埩äžã§ã¯ãªããreadFiles
é¢æ°ãè¿ããããšãã«å®è¡ãããŸãã readFiles
ãè¿ããªãå Žåããã¡ã€ã«èšè¿°åã¯æ°žä¹
ã«éãããŸãŸã«ãªãããªãŒã¯ãçºçããŸãã
ãã®åé¡ã解決ããããã®äžè¬çãªæ段ã®1ã€ã¯ã defer
ã®åŸã«ãåå埩äžã«åŒã³åºãããäžäœãããã¯ã®é¢æ°ãäœæããããšã§ãã
func readFiles(ch <-chan string) error {
for path := range ch {
if err := readFile(path); err != nil {
return err
}
}
return nil
}
func readFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// ãã¡ã€ã«ã®åŠçããã
return nil
}
å¥ã®è§£æ±ºçã¯ãreadFile
é¢æ°ãã¯ããŒãžã£ã«ããããšã§ãããæ¬è³ªçã«ã¯åãã§ããå¥ã®äžäœãããã¯ã®é¢æ°ãè¿œå ããŠãåå埩äžã« defer
åŒã³åºããå®è¡ããŸãã
æåå
ã«ãŒã³ãç解ããŠããªã (#36)
èŠçŽ
ã«ãŒã³ã Unicode ã³ãŒããã€ã³ãã®æŠå¿µã«å¯Ÿå¿ããè€æ°ã®ãã€ãã§æ§æãããå¯èœæ§ãããããšãç解ããããšã¯ã Go éçºè ãæååãæ£ç¢ºã«æäœããããã«äžå¯æ¬ ã§ãã
Goèšèªã§ã¯ã«ãŒã³ãããããå Žæã«äœ¿çšãããããã次ã®ç¹ãç解ããããšãéèŠã§ãã
- æåã»ããã¯æåã®éåã§ããããšã³ã³ãŒãã£ã³ã°ã¯æåã»ããããã€ããªã«å€æããæ¹æ³ãèšè¿°ããŸãã
- Goèšèªã§ã¯ãæååã¯ä»»æã®ãã€ãã®äžå€ã¹ã©ã€ã¹ãåç §ããŸãã
- Goèšèªã®ãœãŒã¹ã³ãŒã㯠UTF-8 ã§ãšã³ã³ãŒããããŠããŸãããããã£ãŠããã¹ã®æååãªãã©ã«ã¯ UTF-8 æååã§ãããã ããæååã«ã¯ä»»æã®ãã€ããå«ãŸããå¯èœæ§ããããããæååãïŒãœãŒã¹ã³ãŒãã§ã¯ãªãïŒä»ã®å ŽæããååŸãããå Žåããã®æååã UTF-8 ãšã³ã³ãŒãã£ã³ã°ã«åºã¥ããŠããä¿èšŒã¯ãããŸããã
rune
㯠Unicode ã³ãŒããã€ã³ãã®æŠå¿µã«å¯Ÿå¿ããåäžã®å€ã§è¡šãããã¢ã€ãã ãæå³ããŸãã- UTF-8 ã䜿çšãããšãUnicode ã³ãŒããã€ã³ãã 1 ïœ 4 ãã€ãã«ãšã³ã³ãŒãã§ããŸãã
- Goèšèªã§æååã«å¯ŸããŠ
len()
ã䜿çšãããšãã«ãŒã³æ°ã§ã¯ãªããã€ãæ°ãè¿ãããŸãã
æååã«å¯Ÿããäžæ£ãªå埩åŠç (#37)
èŠçŽ
range
æŒç®åã䜿çšããŠæååãå埩åŠçãããšãã«ãŒã³ã®ãã€ãã·ãŒã±ã³ã¹ã®éå§ã€ã³ããã¯ã¹ã«å¯Ÿå¿ããã€ã³ããã¯ã¹ã䜿çšããŠã«ãŒã³ãå埩åŠçãããŸããç¹å®ã®ã«ãŒã³ã€ã³ããã¯ã¹ïŒ 3 çªç®ã®ã«ãŒã³ãªã©ïŒã«ã¢ã¯ã»ã¹ããã«ã¯ãæååã []rune
ã«å€æããŸãã
æååã®å埩åŠçã¯ãéçºè ã«ãšã£ãŠäžè¬çãªæäœã§ãããããããæååå ã®åã«ãŒã³ã«å¯ŸããŠæäœãå®è¡ããããç¹å®ã®éšåæååãæ€çŽ¢ããç¬èªã®é¢æ°ãå®è£ ããå¿ èŠãããã§ããããã©ã¡ãã®å Žåããæååã®ç°ãªãã«ãŒã³ãå埩åŠçããå¿ èŠããããŸããããããå埩åŠçãã©ã®ããã«æ©èœãããã«ã€ããŠã¯å°æããããã§ãã
次ã®äŸãèããŠã¿ãŸãããã
s := "hêllo"
for i := range s {
fmt.Printf("position %d: %c\n", i, s[i])
}
fmt.Printf("len=%d\n", len(s))
æ··ä¹±ãæãå¯èœæ§ã®ãã 3 ç¹ãåãäžããŸãããã
- 2 çªç®ã®ã«ãŒã³ã¯ãåºåã§ã¯ ê ã§ã¯ãªã à ã«ãªããŸãã
- position 1 ãã position 3 ã«ãžã£ã³ãããŸããã position 2 ã«ã¯äœãããã®ã§ããããã
- len 㯠6 ãè¿ããŸãããs ã«ã¯ 5 ã€ã®ã«ãŒã³ããå«ãŸããŠããŸããã
çµæã®æåŸããèŠãŠãããŸããããlen ã¯ã«ãŒã³æ°ã§ã¯ãªããæååå
ã®ãã€ãæ°ãè¿ãããšã¯ãã§ã«è¿°ã¹ãŸãããæååãªãã©ã«ã s
ã«å²ãåœãŠãŠãããããs
㯠UTF-8 æååã§ããäžæ¹ãç¹æ®æåãêã㯠1 ãã€ãã§ãšã³ã³ãŒããããŸããã 2 ãã€ãå¿
èŠã§ãããããã£ãŠãlen(s)
ãåŒã³åºããš 6 ãè¿ãããŸãã
åã®äŸã§ã¯ãåã«ãŒã³ãå埩åŠçããŠããªãããšãç解ããå¿ èŠããããŸãã代ããã«ãã«ãŒã³ã®åéå§ã€ã³ããã¯ã¹ãå埩åŠçããŸãã
s[i]
ãåºåããŠã i çªç®ã®ã«ãŒã³ã¯åºåãããŸãããã€ã³ããã¯ã¹ i
ã®ãã€ãã® UTF-8 è¡šçŸãåºåããŸãããããã£ãŠã hêllo
ã®ä»£ããã« hÃllo
ãåºåããããŸãã
ããŸããŸãªã«ãŒã³æåããã¹ãŠåºåãããå Žåã¯ã range
æŒç®åã® value èŠçŽ ã䜿çšããããšãã§ããŸãã
ãŸãã¯ãæååãã«ãŒã³ã®ã¹ã©ã€ã¹ã«å€æãããããå埩åŠçããããšãã§ããŸãã
ãã®è§£æ±ºçã§ã¯ã以åã®è§£æ±ºçãšæ¯èŒããŠå®è¡æã®ãªãŒããŒããããçºçããããšã«æ³šæããŠãã ãããå®éãæååãã«ãŒã³ã®ã¹ã©ã€ã¹ã«å€æããã«ã¯ãè¿œå ã®ã¹ã©ã€ã¹ãå²ãåœãŠããã€ããã«ãŒã³ã«å€æããå¿ èŠããããŸããæååã®ãã€ãæ°ã n ãšãããšãæéèšç®é㯠O(n) ã«ãªããŸãããããã£ãŠããã¹ãŠã®ã«ãŒã³ãå埩åŠçããå Žåã¯ãæåã®è§£æ±ºçã䜿çšããã¹ãã§ãã
ãã ããæåã®æ¹æ³ã䜿çšããŠæååã® i çªç®ã®ã«ãŒã³ã«ã¢ã¯ã»ã¹ãããå Žåã¯ãã«ãŒã³ã€ã³ããã¯ã¹ã«ã¢ã¯ã»ã¹ã§ããŸããã代ããã«ããã€ãã·ãŒã±ã³ã¹å ã®ã«ãŒã³ã®éå§ã€ã³ããã¯ã¹ãããããŸãã
trim é¢æ°ã®èª€çš (#38)
èŠçŽ
strings.TrimRight
ã» strings.TrimLeft
ã¯ãæå®ãããã»ããã«å«ãŸãããã¹ãŠã®æ«å°Ÿã»å
é ã®ã«ãŒã³ãåé€ããŸããã strings.TrimSuffix
ã» strings.TrimPrefix
ã¯ãæå®ãããæ¥å°ŸèŸã»æ¥é èŸã®ãªãæååãè¿ããŸãã
ããšãã°
㯠123 ãåºåããŸã
éã«ã strings.TrimLeft
ã¯ãã»ããã«å«ãŸããå
é ã®ã«ãŒã³ããã¹ãŠåé€ããŸãã
äžæ¹ãstrings.TrimSuffix
ã» strings.TrimPrefix
ã¯ãæå®ãããæ«å°Ÿã®æ¥å°ŸèŸã»æ¥é èŸãé€ããæååãè¿ããŸãã
æé©åãäžååãªæååã®é£çµ (#39)
èŠçŽ
æååã®ãªã¹ãã®é£çµã¯ãå埩ããšã«æ°ããæååãå²ãåœãŠãããªãããã«ãstrings.Builder
ã䜿çšããŠè¡ãå¿
èŠããããŸãã
+=
æŒç®åãçšããŠã¹ã©ã€ã¹ã®ãã¹ãŠã®æååèŠçŽ ãé£çµãã concat
é¢æ°ãèããŠã¿ãŸãããã
func concat(values []string) string {
s := ""
for _, value := range values {
s += value
}
return s
}
åå埩äžã«ã +=
æŒç®å㯠s
ãš value æååãé£çµããŸããäžèŠãããšããã®é¢æ°ã¯ééã£ãŠããªãããã«èŠãããããããŸããããããããã®å®è£
ã¯ãæååã®æ žãšãªãç¹æ§ã®1ã€ã§ããäžå€æ§ãå¿ããŠããŸãããããã£ãŠãåå埩ã§ã¯ s
ã¯æŽæ°ãããŸãããã¡ã¢ãªå
ã«æ°ããæååãåå²ãåœãŠããããããã®é¢æ°ã®ããã©ãŒãã³ã¹ã«å€§ããªåœ±é¿ãäžããŸãã
幞ããªããšã«ã strings.Builder
ãçšããããšã§ããã®åé¡ã«å¯ŸåŠãã解決çããããŸãã
func concat(values []string) string {
sb := strings.Builder{}
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
åå埩äžã«ãvalue ã®å
容ãå
éšãããã¡ã«è¿œå ãã WriteString
ã¡ãœãããåŒã³åºããŠçµæã®æååãæ§ç¯ããã¡ã¢ãªã®ã³ããŒãæå°éã«æããããšãã§ããŸããã
è£è¶³
WriteString
㯠2 çªç®ã®åºåãšããŠãšã©ãŒãè¿ããŸãããæå³çã«ç¡èŠããŸããããå®éããã®ã¡ãœãã㯠nil ãšã©ãŒä»¥å€ãè¿ãããšã¯ãããŸãããã§ã¯ããã®ã¡ãœãããã·ã°ããã£ã®äžéšãšããŠãšã©ãŒãè¿ãç®çã¯äœã§ãããããstrings.Builder
㯠io.StringWriter
ã€ã³ã¿ãã§ãŒã¹ãå®è£
ããŠãããããã«ã¯ WriteString(s string) (n int, err error)
ãšãã1ã€ã®ã¡ãœãããå«ãŸããŠããŸãããããã£ãŠããã®ã€ã³ã¿ãã§ãŒã¹ã«æºæ ããã«ã¯ãWriteString
ã¯ãšã©ãŒãè¿ããªããã°ãªããªãã®ã§ãã
å
éšçã«ã¯ãstrings.Builder
ã¯ãã€ãã¹ã©ã€ã¹ãä¿æããŸãã WriteString
ãåŒã³åºããã³ã«ããã®ã¹ã©ã€ã¹ã«è¿œå ããåŒã³åºããè¡ãããŸããããã«ã¯2ã€ã®åœ±é¿ããããŸãããŸãã append
ã®åŒã³åºããè¡çªç¶æ
ãåŒãèµ·ããå¯èœæ§ãããããããã®æ§é äœã¯åæã«äœ¿çšãããã¹ãã§ã¯ãããŸããã2çªç®ã®åœ±é¿ã¯ã éå¹çãªã¹ã©ã€ã¹ã®åæå (#21) ã§èŠããã®ã§ããã¹ã©ã€ã¹ã®å°æ¥ã®é·ãããã§ã«ããã£ãŠããå Žåã¯ããããäºåã«å²ãåœãŠãå¿
èŠããããŸãããã®ããã«ãstrings.Builder
ã¯å¥ã® n
ãã€ãã®ããã®ã¹ããŒã¹ãä¿èšŒããã¡ãœãã Grow(n int)
ãæã£ãŠããŸãã
func concat(values []string) string {
total := 0
for i := 0; i < len(values); i++ {
total += len(values[i])
}
sb := strings.Builder{}
sb.Grow(total) (2)
for _, value := range values {
_, _ = sb.WriteString(value)
}
return sb.String()
}
ãã³ãããŒã¯ãå®è¡ã㊠3 ã€ã®ããŒãžã§ã³ïŒ +=
ã䜿çšãã V1 ãäºåå²ãåœãŠãªã㧠strings.Builder{}
ã䜿çšãã V2 ãäºåå²ãåœãŠããã® strings.Builder{}
ã䜿çšãã V3 ïŒãæ¯èŒããŠã¿ãŸããããå
¥åã¹ã©ã€ã¹ã«ã¯ 1,000 åã®æååãå«ãŸããŠãããåæååã«ã¯ 1,000 ãã€ããå«ãŸããŠããŸãã
BenchmarkConcatV1-4 16 72291485 ns/op
BenchmarkConcatV2-4 1188 878962 ns/op
BenchmarkConcatV3-4 5922 190340 ns/op
ã芧ã®ãšãããææ°ããŒãžã§ã³ãæãå¹ççã§ãV1 ãã 99% ãV2 ãã 78% é«éã§ãã
strings.Builder
ã¯ãæååã®ãªã¹ããé£çµããããã®è§£æ±ºçãšããŠæšå¥šãããŸããéåžžãããã¯ã«ãŒãå
ã§äœ¿çšããå¿
èŠããããŸããããã€ãã®æåå ïŒååãšå§ãªã©ïŒãé£çµããã ãã®å Žåã strings.Builder
ã®äœ¿çšã¯ã +=
æŒç®åã fmt.Sprintf
ãšæ¯ã¹ãŠå¯èªæ§ãäœããªãããã§ãã
ç¡é§ãªæååå€æ (#40)
èŠçŽ
bytes
ããã±ãŒãžã¯ strings
ããã±ãŒãžãšåãæäœãæäŸããŠãããããšãèŠããŠãããšãäœåãªãã€ãã»æååå€æãé¿ããããšãã§ããŸãã
æååãŸã㯠[]byte
ãæ±ãããšãéžæããå Žåãã»ãšãã©ã®ããã°ã©ããŒã¯å©äŸ¿æ§ã®ããã«æååã奜ãåŸåããããŸããããããã»ãšãã©ã® I/O ã¯å®éã«ã¯ []byte
ã§è¡ãããŸããããšãã°ãio.Reader
ãio.Writer
ãããã³ io.ReadAll
ã¯æååã§ã¯ãªã []byte
ãåŠçããŸãã
æååãš []byte
ã®ã©ã¡ããæ±ãã¹ããè¿·ã£ããšãã[]byte
ãæ±ãæ¹ãå¿
ãããé¢åã ãšããããã§ã¯ãªãããšãæãåºããŠãã ãããstrings ããã±ãŒãžãããšã¯ã¹ããŒãããããã¹ãŠã®é¢æ°ã«ã¯ãbytes
ããã±ãŒãžã«ä»£æ¿æ©èœããããŸãã Split
ãCount
ãContains
ãIndex
ãªã©ã§ãããããã£ãŠãI/O ãå®è¡ããŠãããã©ããã«é¢ä¿ãªããæååã®ä»£ããã«ãã€ãã䜿çšããŠã¯ãŒã¯ãããŒå
šäœãå®è£
ã§ããè¿œå ã®å€æã³ã¹ããåé¿ã§ãããã©ãããæåã«ç¢ºèªããŸãããã
éšåæååãšã¡ã¢ãªãªãŒã¯ (#41)
èŠçŽ
éšåæååã®ä»£ããã«ã³ããŒã䜿çšãããšãéšåæååæäœã«ãã£ãŠè¿ãããæååãåããã€ãé åã«ãã£ãŠãµããŒãããããããã¡ã¢ãªãªãŒã¯ãé²ãããšãã§ããŸãã
ã¹ã©ã€ã¹ãšã¡ã¢ãªãªãŒã¯ (#26) ã§ã¯ãã¹ã©ã€ã¹ãŸãã¯é åã®ã¹ã©ã€ã¹ãã¡ã¢ãªãªãŒã¯ã®ç¶æ³ãåŒãèµ·ããå¯èœæ§ãããããšã確èªããŸããããã®ååã¯ãæååããã³éšåæååã®æäœã«ãåœãŠã¯ãŸããŸãã
Goèšèªã§éšåæååæäœã䜿çšãããšãã¯ã2 ã€ã®ããšã«çæããå¿
èŠããããŸãããŸããæäŸãããééã¯ã«ãŒã³æ°ã§ã¯ãªãããã€ãæ°ã«åºã¥ããŠããŸãã次ã«ãçµæã®éšåæååãæåã®æååãšåããããã³ã°é
åãå
±æãããããéšåæååæäœã«ããã¡ã¢ãªãªãŒã¯ãçºçããå¯èœæ§ããããŸãããããé²ãæ¹æ³ã¯ãæååã®ã³ããŒãæåã§å®è¡ããããGo 1.18 ããå®è£
ãããŠãã strings.Clone
ã䜿çšããããšã§ãã
é¢æ°ãšã¡ãœãã
ã©ã®åã®ã¬ã·ãŒããŒã䜿çšããã°ãããããã£ãŠããªã (#42)
èŠçŽ
å€ã¬ã·ãŒããŒãšãã€ã³ã¿ã¬ã·ãŒããŒã®ã©ã¡ãã䜿çšãããã¯ãã©ã®åãªã®ããå€åãããå¿ èŠããããã©ãããã³ããŒã§ããªããã£ãŒã«ããå«ãŸããŠãããã©ããããªããžã§ã¯ãã¯ã©ãããã倧ããã®ãããªã©ã®èŠçŽ ã«åºã¥ããŠæ±ºå®ããå¿ èŠããããŸããåãããªãå Žåã¯ããã€ã³ã¿ã¬ã·ãŒãã䜿çšããŠãã ããã
å€ã¬ã·ãŒããŒãšãã€ã³ã¿ã¬ã·ãŒããŒã®ã©ã¡ããéžæãããã¯ãå¿ ãããç°¡åã§ã¯ãããŸãããéžæã«åœ¹ç«ã€ããã€ãã®æ¡ä»¶ã«ã€ããŠèª¬æããŸãããã
ãã€ã³ã¿ã¬ã·ãŒããŒã§ãªããã°ãªããªã ãšã
- ã¡ãœãããã¬ã·ãŒããŒãå€åãããå¿ èŠãããå Žåããã®ã«ãŒã«ã¯ãåä¿¡åŽãã¹ã©ã€ã¹ã§ãããã¡ãœãããèŠçŽ ãè¿œå ããå¿ èŠãããå Žåã«ãæå¹ã§ãã
- ã¡ãœããã¬ã·ãŒããŒã«ã³ããŒã§ããªããã£ãŒã«ããå«ãŸããŠããå Žåãsync ããã±ãŒãžã®åéšåã¯ãã®äžäŸã«ãªããŸãïŒ sync åã®ã³ã㌠(#74) ãåç §ïŒã
ãã€ã³ã¿ã¬ã·ãŒããŒã§ããã¹ã ãšã
- ã¬ã·ãŒããŒã倧ããªãªããžã§ã¯ãã®å Žåããã€ã³ã¿ã䜿çšãããšã倧èŠæš¡ãªã³ããŒã®äœæãé²æ¢ããããããåŒã³åºããããå¹ççã«ãªããŸããã©ããããã®å€§ãããªã®ã確蚌ããªãå Žåã¯ããã³ãããŒã¯ã解決çã«ãªãå¯èœæ§ããããŸããå€ãã®èŠå ã«äŸåãããããç¹å®ã®ãµã€ãºãæå®ããããšã¯ã»ãšãã©äžå¯èœã§ãã
å€ã¬ã·ãŒããŒã§ãªããã°ãªããªã ãšã
- ã¬ã·ãŒããŒã®äžå€æ§ã匷å¶ããå¿ èŠãããå Žåã
- ã¬ã·ãŒããŒãããããé¢æ°ããã£ãã«ã®å Žåããã以å€ã®å Žåã¯ã³ã³ãã€ã«ãšã©ãŒãçºçããŸãã
å€ã¬ã·ãŒããŒã§ããã¹ã ãšã
- ã¬ã·ãŒããŒãå€åãããå¿ èŠã®ãªãã¹ã©ã€ã¹ã®å Žåã
- ã¬ã·ãŒããŒãã
time.Time
ãªã©ã®å°ããªé åãŸãã¯æ§é äœã§ãå¯å€ãã£ãŒã«ããæããªãå€åã§ããå Žåã - ã¬ã·ãŒããŒã
int
ãfloat64
ããŸãã¯string
ãªã©ã®åºæ¬åã®å Žåã
ãã¡ãããç¹æ®ãªã±ãŒã¹ã¯åžžã«ååšããããããã¹ãŠãç¶²çŸ ããããšã¯äžå¯èœã§ããããã®ã»ã¯ã·ã§ã³ã®ç®æšã¯ãã»ãšãã©ã®ã±ãŒã¹ãã«ããŒããããã®ã¬ã€ãã³ã¹ãæäŸããããšã§ããéåžžã¯ãããããªãæ£åœãªçç±ããªãéããå€ã¬ã·ãŒããŒã䜿çšããŠééããããŸãããåãããªãå Žåã¯ããã€ã³ã¿ã¬ã·ãŒãã䜿çšããå¿ èŠããããŸãã
ååä»ãçµæãã©ã¡ãŒã¿ããŸã£ãã䜿çšããŠããªã (#43)
èŠçŽ
ååä»ãçµæãã©ã¡ãŒã¿ãŒã®äœ¿çšã¯ãç¹ã«è€æ°ã®çµæãã©ã¡ãŒã¿ãŒãåãåãæã€å Žåãé¢æ°ã»ã¡ãœããã®èªã¿ããããåäžãããå¹ççãªæ¹æ³ã§ããå Žåã«ãã£ãŠã¯ãååä»ãçµæãã©ã¡ãŒã¿ã¯ãŒãå€ã«åæåãããããããã®æ¹æ³ã䟿å©ã§ããããããšããããŸãããã ãæœåšçãªå¯äœçšã«ã¯æ³šæããŠãã ããã
é¢æ°ãŸãã¯ã¡ãœããã§ãã©ã¡ãŒã¿ãè¿ããšãããããã®ãã©ã¡ãŒã¿ã«ååãä»ããŠãéåžžã®å€æ°ãšããŠäœ¿çšã§ããŸããçµæãã©ã¡ãŒã¿ãŒã«ååãä»ãããšãé¢æ°ã»ã¡ãœããã®éå§æã«ãã®ãã©ã¡ãŒã¿ãŒã¯ãŒãå€ã«åæåãããŸããååä»ãçµæãã©ã¡ãŒã¿ã䜿çšãããšã ããåºãã® return æïŒåŒæ°ãªãïŒ ãåŒã³åºãããšãã§ããŸãããã®å Žåãçµæãã©ã¡ãŒã¿ã®çŸåšã®å€ãæ»ãå€ãšããŠäœ¿çšãããŸãã
以äžã¯ãååä»ãçµæãã©ã¡ãŒã¿ b
ãçšããäŸã§ãã
ãã®äŸã§ã¯ãçµæãã©ã¡ãŒã¿ã«åå b
ãä»ããŠããŸããåŒæ°ãªã㧠return ãåŒã³åºããšãb
ã®çŸåšã®å€ãè¿ãããŸãã
å Žåã«ãã£ãŠã¯ãååä»ãã®çµæãã©ã¡ãŒã¿ãŒã«ãã£ãŠå¯èªæ§ãåäžããããšããããŸããããšãã°ã2 ã€ã®ãã©ã¡ãŒã¿ãŒãåãåã§ããå Žåãªã©ã§ãããã®ä»ã«ããå©äŸ¿æ§ã®ããã«çšããããšãã§ããŸããããã«ãæ確ãªå©ç¹ãããå Žåã¯ãæ éã«ãªããªãããååä»ãçµæãã©ã¡ãŒã¿ã䜿çšããã¹ãã§ãã
ååä»ãçµæãã©ã¡ãŒã¿ã«ããäºæ³å€ã®å¯äœçš (#44)
èŠçŽ
#43 ãåç §ããŠãã ããã
ååä»ãçµæãã©ã¡ãŒã¿ãç¶æ³ã«ãã£ãŠã¯åœ¹ç«ã€çç±ã«ã€ããŠèª¬æããŸããã ãã ãããããã¯ãŒãå€ã«åæåããããããååã«æ³šæããªããšã軜埮ãªãã°ãçºçããå¯èœæ§ããããŸããããšãã°ããã®ã³ãŒãã¯ã©ããééã£ãŠããã§ããããã
func (l loc) getCoordinates(ctx context.Context, address string) (
lat, lng float32, err error) {
isValid := l.validateAddress(address) (1)
if !isValid {
return 0, 0, errors.New("invalid address")
}
if ctx.Err() != nil { (2)
return 0, 0, err
}
// 座æšãååŸããŠè¿ã
}
äžç¥ããã ãã§ã¯ãšã©ãŒã¯æããã§ã¯ãªããããããŸãããif ctx.Err() != nil
ã¹ã³ãŒãã§è¿ããããšã©ãŒã¯ err
ã§ããããããerr
å€æ°ã«ã¯å€ãå²ãåœãŠãŠããŸãããerror
åã®ãŒãå€ã nil
ã«å²ãåœãŠããããŸãŸã§ãããããã£ãŠããã®ã³ãŒãã¯åžžã« nil ãšã©ãŒãè¿ããŸãã
ååä»ãçµæãã©ã¡ãŒã¿ã䜿çšããå Žåãåãã©ã¡ãŒã¿ã¯ãŒãå€ã«åæåãããããšã«æ³šæããŠãã ããããã®ã»ã¯ã·ã§ã³ã§èª¬æããããã«ãããã«ãããèŠã€ããã®ãå¿ ãããç°¡åã§ã¯ãªã軜埮ãªãã°ãçºçããå¯èœæ§ããããŸããããã«ãæœåšçãªå¯äœçšãé¿ããããã«ãååä»ãçµæãã©ã¡ãŒã¿ãŒã䜿çšãããšãã¯æ³šæããŠãã ããã
nil ã¬ã·ãŒããŒãè¿ã (#45)
èŠçŽ
ã€ã³ã¿ãã§ãŒã¹ãè¿ããšãã¯ãnil ãã€ã³ã¿ãè¿ãã®ã§ã¯ãªããæ瀺ç㪠nil å€ãè¿ãããã«æ³šæããŠãã ãããããããªããã°ãæå³ããªãçµæãçºçããåŒã³åºãå ã nil ã§ã¯ãªãå€ãåãåãå¯èœæ§ããããŸãã
é¢æ°å ¥åã«ãã¡ã€ã«åã䜿çšããŠãã (#46)
èŠçŽ
ãã¡ã€ã«åã®ä»£ããã« io.Reader
åãåãåãããã«é¢æ°ãèšèšãããšãé¢æ°ã®åå©çšæ§ãåäžãããã¹ãã容æã«ãªããŸãã
ãã¡ã€ã«åããã¡ã€ã«ããèªã¿åãããã®é¢æ°å
¥åãšããŠåãå
¥ããããšã¯ãã»ãšãã©ã®å Žåããã³ãŒãã®èãããšã¿ãªãããã¹ãã§ãïŒ os.Open
ãªã©ã®ç¹å®ã®é¢æ°ãé€ãïŒãè€æ°ã®ãã¡ã€ã«ãäœæããããšã«ã«ãªãããããããåäœãã¹ããããè€éã«ãªãå¯èœæ§ãããããã§ãããŸããé¢æ°ã®åå©çšæ§ãäœäžããŸã ïŒãã ãããã¹ãŠã®é¢æ°ãåå©çšãããããã§ã¯ãããŸããïŒã io.Reader
ã€ã³ã¿ãã§ãŒã¹ã䜿çšãããšãããŒã¿ãœãŒã¹ãæœè±¡åãããŸããå
¥åããã¡ã€ã«ãæååãHTTP ãªã¯ãšã¹ããgRPC ãªã¯ãšã¹ãã®ãããã§ãããã«é¢ä¿ãªããå®è£
ã¯åå©çšã§ããç°¡åã«ãã¹ãã§ããŸãã
defer
åŒæ°ãšã¬ã·ãŒããŒãã©ã®ããã«è©äŸ¡ãããããç¥ããªãïŒåŒæ°ã®è©äŸ¡ããã€ã³ã¿ãŒãããã³å€ã¬ã·ãŒããŒïŒ (#47)
èŠçŽ
ãã€ã³ã¿ã defer
é¢æ°ã«æž¡ãããšãšãåŒã³åºããã¯ããŒãžã£å
ã«ã©ããããããšããåŒæ°ãšã¬ã·ãŒããŒã®å³æè©äŸ¡ãå
æããããã«å®çŸå¯èœãªè§£æ±ºçã§ãã
defer
é¢æ°ã§ã¯ãåŒæ°ã¯ãäžäœãããã¯ã®é¢æ°ãæ»ã£ãŠããã§ã¯ãªããããã«è©äŸ¡ãããŸããããšãã°ããã®ã³ãŒãã§ã¯ãåžžã«åãã¹ããŒã¿ã¹ââ空ã®æååââ㧠notify
ãš incrementCounter
ãåŒã³åºããŸãã
const (
StatusSuccess = "success"
StatusErrorFoo = "error_foo"
StatusErrorBar = "error_bar"
)
func f() error {
var status string
defer notify(status)
defer incrementCounter(status)
if err := foo(); err != nil {
status = StatusErrorFoo
return err
}
if err := bar(); err != nil {
status = StatusErrorBar
return err
}
status = StatusSuccess
return nil
}
ãããã«ãnotify(status)
ãš incrementCounter(status)
ã defer
é¢æ°ãšããŠåŒã³åºããŠããŸãããããã£ãŠãGoèšèªã¯ãdefer ã䜿çšãã段é㧠f
ãã¹ããŒã¿ã¹ã®çŸåšã®å€ãè¿ããšããããã®åŒã³åºãã®å®è¡ãé
ããã空ã®æååãæž¡ããŸãã
defer
ã䜿ãç¶ãããå Žåã®äž»ãªæ¹æ³ã¯ 2 ã€ãããŸãã
æåã®è§£æ±ºçã¯æååãã€ã³ã¿ãæž¡ãããšã§ãã
func f() error {
var status string
defer notify(&status)
defer incrementCounter(&status)
// é¢æ°ã®ãã以å€ã®éšåã¯å€æŽãªã
}
defer
ã䜿çšãããšãåŒæ°ïŒããã§ã¯ã¹ããŒã¿ã¹ã®ã¢ãã¬ã¹ïŒãããã«è©äŸ¡ãããŸããã¹ããŒã¿ã¹èªäœã¯é¢æ°å
šäœã§å€æŽãããŸããããã®ã¢ãã¬ã¹ã¯å²ãåœãŠã«é¢ä¿ãªãäžå®ã®ãŸãŸã§ãããã£ãŠãnotify
ãŸã㯠incrementCounter
ãæååãã€ã³ã¿ã«ãã£ãŠåç
§ãããå€ã䜿çšããå ŽåãæåŸ
ã©ããã«åäœããŸãããã ãããã®è§£æ±ºçã§ã¯ 2 ã€ã®é¢æ°ã®ã·ã°ããã£ãå€æŽããå¿
èŠãããããããåžžã«å¯èœã§ãããšã¯éããŸããã
å¥ã®è§£æ±ºçããããŸãââã¯ããŒãžã£ïŒæ¬äœã®å€éšããå€æ°ãåç
§ããå¿åé¢æ°å€ïŒã defer
æãšããŠåŒã³åºãããšã§ãã
func f() error {
var status string
defer func() {
notify(status)
incrementCounter(status)
}()
// é¢æ°ã®ãã以å€ã®éšåã¯å€æŽãªã
}
ããã§ã¯ãnotify
ãš incrementCounter
ã®äž¡æ¹ã®åŒã³åºããã¯ããŒãžã£å
ã«ã©ããããŸãããã®ã¯ããŒãžã£ã¯ãæ¬äœã®å€éšããã¹ããŒã¿ã¹å€æ°ãåç
§ããŸããããã«ãstatus
ã¯ãdefer
ãåŒã³åºãããšãã§ã¯ãªããã¯ããŒãžã£ãå®è¡ããããšãã«è©äŸ¡ãããŸãããã®è§£æ±ºçã¯æ£ããæ©èœããäžã«ãã·ã°ããã£ãå€æŽããããã« notify
ã incrementCounter
ãå¿
èŠãšããŸããã
ãã®åäœã¯ã¡ãœããã¬ã·ãŒããŒã«ãé©çšãããããšã«ã泚æããŠãã ãããã¬ã·ãŒããŒã¯ããã«è©äŸ¡ãããŸãã
ãšã©ãŒåŠç
ããã㯠(#48)
èŠçŽ
panic
ã®äœ¿çšã¯ãGoèšèªã§ãšã©ãŒã«å¯ŸåŠããããã®æ段ã§ãããã ããããã¯å埩äžèœãªç¶æ³ã§ã®ã¿äœ¿çšããããã«ããŠãã ãããããšãã°ããã¥ãŒãã³ãšã©ãŒãéç¥ããå Žåããå¿
é ã®äŸåé¢ä¿ã®èªã¿èŸŒã¿ã«å€±æããå Žåãªã©ã§ãã
Goèšèªã§ã¯ãpanic ã¯éåžžã®æµããåæ¢ããçµã¿èŸŒã¿é¢æ°ã§ãã
ãã®ã³ãŒã㯠a ãåºåããb ãåºåããåã«åæ¢ããŸãã
panic ã®äœ¿çšã¯æ
éã«ãã¹ãã§ãã代衚çãªã±ãŒã¹ã 2 ã€ããã1 ã€ã¯ãã¥ãŒãã³ãšã©ãŒãéç¥ããå ŽåïŒäŸ: sql.Register
ãã©ã€ããŒã nil
ãŸãã¯æ¢ã«ç»é²ãããŠããå Žåã« panic ãèµ·ãããŸãïŒããã 1 ã€ã¯ã¢ããªã±ãŒã·ã§ã³ãå¿
é ã®äŸåé¢ä¿ã®çæã«å€±æããå Žåã§ããçµæãšããŠãäŸå€çã«ã¢ããªã±ãŒã·ã§ã³ãåæ¢ããŸãããã以å€ã®ã»ãšãã©ã®å Žåã«ãããŠã¯ããšã©ãŒåŠçã¯ãæåŸã®æ»ãåŒæ°ãšããŠé©åãªãšã©ãŒåãè¿ãé¢æ°ãéããŠè¡ãã¹ãã§ãã
ãšã©ãŒãã©ãããã¹ããšããç¥ããªã (#49)
èŠçŽ
ãšã©ãŒãã©ãããããšããšã©ãŒãããŒã¯ããããè¿œå ã®ã³ã³ããã¹ããæäŸãããã§ããŸãããã ãããšã©ãŒã©ããã³ã°ã«ãããåŒã³åºãå ããœãŒã¹ãšã©ãŒãå©çšã§ããããã«ãªããããæœåšçãªçµåãçºçããŸãããããé¿ãããå Žåã¯ããšã©ãŒã©ããã³ã°ã䜿çšããªãã§ãã ããã
Go 1.13 以éã%w ãã£ã¬ã¯ãã£ãã䜿çšããã°ç°¡åã«ãšã©ãŒãã©ããã§ããããã«ãªããŸããããšã©ãŒã©ããã³ã°ãšã¯ããœãŒã¹ãšã©ãŒã䜿çšã§ããããã«ããã©ãããŒã³ã³ããå ã§ãšã©ãŒãã©ãããŸãã¯ããã¯ããããšã§ããäžè¬ã«ããšã©ãŒã©ããã³ã°ã®äž»ãªäœ¿çšäŸã¯æ¬¡ã® 2 ã€ã§ãã
- ãšã©ãŒã«ããã«ã³ã³ããã¹ããå ãã
- ãšã©ãŒãç¹å®ã®ãšã©ãŒãšããŠããŒã¯ãã
ãšã©ãŒãåŠçãããšãããšã©ãŒãã©ãããããã©ããã決å®ã§ããŸããã©ããã³ã°ãšã¯ããšã©ãŒã«ããã«ã³ã³ããã¹ããè¿œå ãããããšã©ãŒãç¹å®ã®ã¿ã€ããšããŠããŒã¯ãããããããšã§ãããšã©ãŒãããŒã¯ããå¿ èŠãããå Žåã¯ãç¬èªã®ãšã©ãŒåãäœæããå¿ èŠããããŸããã§ãããæ°ãã«ã³ã³ããã¹ããå ãããã ãã®å Žåã¯ãæ°ãããšã©ãŒåãäœæããå¿ èŠããªãããã%w ãã£ã¬ã¯ãã£ããæå®ã㊠fmt.Errorf ã䜿çšããŸãããããã ãããšã©ãŒã©ããã³ã°ã«ãããåŒã³åºãå ããœãŒã¹ãšã©ãŒãå©çšã§ããããã«ãªããããæœåšçãªçµåãçããŸãããããé¿ãããå Žåã¯ããšã©ãŒã®ã©ããã³ã°ã§ã¯ãªãããšã©ãŒã®å€æã䜿çšããå¿ èŠããããŸããããšãã°ã%v ãã£ã¬ã¯ãã£ããæå®ãã fmt.Errorf ã䜿çšããŸãã
ãšã©ãŒåã®äžæ£ãªæ¯èŒ (#50)
èŠçŽ
Go 1.13 ã®ãšã©ãŒã©ããã³ã°ã %w
ãã£ã¬ã¯ãã£ããš fmt.Errorf
ã§äœ¿çšããå Žåãåã«å¯Ÿãããšã©ãŒã®æ¯èŒã¯ errors.As
ãéããŠè¡ãå¿
èŠããããŸããããã§ãªããã°ãè¿ããããšã©ãŒãã©ãããããŠããå Žåãè©äŸ¡ã«å€±æããŸãã
ãšã©ãŒå€ã®äžæ£ãªæ¯èŒ (#51)
èŠçŽ
Go 1.13 ã®ãšã©ãŒã©ããã³ã°ã %w
ãã£ã¬ã¯ãã£ããš fmt.Errorf
ã§äœ¿çšããå Žåããšã©ãŒãšå€ã®æ¯èŒã¯ errors.As
ãéããŠè¡ãå¿
èŠããããŸããããã§ãªããã°ãè¿ããããšã©ãŒãã©ãããããŠããå Žåãè©äŸ¡ã«å€±æããŸãã
ã»ã³ããã«ãšã©ãŒã¯ã°ããŒãã«å€æ°ãšããŠå®çŸ©ããããšã©ãŒã®ããšã§ãã
äžè¬ã«ãæ £äŸãšããŠErr
ã§å§ãããã®åŸã«ãšã©ãŒåãç¶ããŸããããã§ã¯ ErrFoo
ã§ããã»ã³ããã«ãšã©ãŒã¯ãäºæããã ãšã©ãŒãã€ãŸãã¯ã©ã€ã¢ã³ãã確èªããããšãæåŸ
ãããšã©ãŒãäŒããŸããäžè¬çãªã¬ã€ãã©ã€ã³ãšããŠ
- äºæããããšã©ãŒã¯ãšã©ãŒå€ïŒã»ã³ããã«ãšã©ãŒïŒãšããŠèšèšããå¿
èŠããããŸãïŒ
var ErrFoo =errors.New("foo")
ã - äºæããªããšã©ãŒã¯ãšã©ãŒåãšããŠèšèšããå¿
èŠããããŸãïŒ
BarError
ã¯error
ã€ã³ã¿ãã§ãŒã¹ãå®è£ ããäžã§type BarError struct { ... }
ã
ã¢ããªã±ãŒã·ã§ã³ã§ %w
ãã£ã¬ã¯ãã£ããš fmt.Errorf
ã䜿çšããŠãšã©ãŒã©ããã䜿çšããå Žåãç¹å®ã®å€ã«å¯Ÿãããšã©ãŒã®ãã§ãã¯ã¯ ==
ã®ä»£ããã« errors.Is
ã䜿çšããŠè¡ããŸããããããã«ãã£ãŠãã»ã³ããã«ãšã©ãŒãã©ãããããŠããå Žåã§ããerrors.Is
ã¯ãããååž°çã«ã¢ã³ã©ãããããã§ãŒã³å
ã®åãšã©ãŒãæäŸãããå€ãšæ¯èŒã§ããŸãã
ãšã©ãŒã® 2 ååŠç (#52)
èŠçŽ
ã»ãšãã©ã®å Žåããšã©ãŒã¯ 1 åã§åŠçãããã¹ãã§ãããšã©ãŒããã°ã«èšé²ããããšã¯ããšã©ãŒãåŠçããããšã§ããããªãã¡ããã°ã«èšé²ããããšã©ãŒãè¿ãããéžæããå¿ èŠããããŸããå€ãã®å Žåããšã©ãŒã©ããã³ã°ã¯ããšã©ãŒã«è¿œå ã®ã³ã³ããã¹ããæäŸãããœãŒã¹ãšã©ãŒãè¿ãããšãã§ããããã解決çã«ãªããŸãã
ãšã©ãŒãè€æ°ååŠçããããšã¯ãç¹ã«Goèšèªã«éãããéçºè ãé »ç¹ã«ãã£ãŠããŸããã¹ã§ããããã«ãããåããšã©ãŒãè€æ°åãã°ã«èšé²ããããããã°ãå°é£ã«ãªãç¶æ³ãçºçããå¯èœæ§ããããŸãã
ãšã©ãŒåŠç㯠1 床ã§æžãŸãã¹ãã ãšããããšãèŠããŠãããŸãããããšã©ãŒããã°ã«èšé²ããããšã¯ããšã©ãŒãåŠçããããšã§ããã€ãŸããè¡ãã¹ãã¯ããã°ã«èšé²ãããããšã©ãŒãè¿ããã®ã©ã¡ããã ãšããããšã§ããããã«ãããã³ãŒããç°¡çŽ åããããšã©ãŒã®ç¶æ³ã«ã€ããŠããé©åãªæŽå¯ãåŸãããŸãããšã©ãŒã©ããã³ã°ã¯ããœãŒã¹ãšã©ãŒãäŒãããšã©ãŒã«ã³ã³ããã¹ããè¿œå ã§ãããããæã䜿ãåæã®è¯ãæ段ã«ãªããŸãã
ãšã©ãŒåŠçãããªã (#53)
èŠçŽ
é¢æ°åŒã³åºãäžã§ãã£ãŠããdefer
é¢æ°å
ã§ãã£ãŠãããšã©ãŒãç¡èŠãããšãã¯ããã©ã³ã¯èå¥åã䜿çšããŠæ確ã«è¡ãã¹ãã§ããããããªããšãå°æ¥ã®èªã¿æããããæå³çã ã£ãã®ãããããšããã¹ã ã£ãã®ãå°æããå¯èœæ§ããããŸãã
defer
ãšã©ãŒãåŠçããªã (#54)
èŠçŽ
å€ãã®å Žåãdefer
é¢æ°ã«ãã£ãŠè¿ããããšã©ãŒãç¡èŠãã¹ãã§ã¯ãããŸãããç¶æ³ã«å¿ããŠãçŽæ¥åŠçããããåŒã³åºãå
ã«äŒããŸãããããããç¡èŠããå Žåã¯ããã©ã³ã¯èå¥åã䜿çšããŠãã ããã
次ã®ã³ãŒããèããŠã¿ãŸãããã
func f() {
// ...
notify() // ãšã©ãŒåŠçã¯çç¥ãããŠããŸã
}
func notify() error {
// ...
}
ä¿å®æ§ã®èŠ³ç¹ããããã®ã³ãŒãã¯ããã€ãã®åé¡ãåŒãèµ·ããå¯èœæ§ããããŸãããã人ããããèªãããšãèããŠã¿ãŸããèªã¿æã¯ãnotify ããšã©ãŒãè¿ãã«ããããããããã®ãšã©ãŒã芪é¢æ°ã«ãã£ãŠåŠçãããªãããšã«æ°ã¥ããŸãããšã©ãŒåŠçãæå³çã§ãããã©ãããæãããŠæšæž¬ã§ããã§ããããã以åã®éçºè ããããåŠçããã®ãå¿ããã®ãããããšãæå³çã«åŠçããã®ããç¥ãããšãã§ããã§ããããã
ãããã®çç±ã«ããããšã©ãŒãç¡èŠãããå Žåããã©ã³ã¯èå¥åïŒ _
ïŒã䜿ãã»ããããŸããã
ã³ã³ãã€ã«ãšå®è¡æéã®ç¹ã§ã¯ããã®æ¹æ³ã¯æåã®ã³ãŒãéšåãšæ¯ã¹ãŠäœãå€ãããŸããããããããã®æ°ããããŒãžã§ã³ã§ã¯ãç§ãã¡ããšã©ãŒã«é¢å¿ããªãããšãæããã«ããŠããŸãããŸãããšã©ãŒãç¡èŠãããçç±ã瀺ãã³ã¡ã³ããè¿œå ããããšãã§ããŸãã
// æ倧ã§ã 1 åã®äŒé
// ããããããšã©ãŒãçºçããå Žåã«ãããã®äžéšã倱ãããããšã¯èš±å®¹ãããŸã
_ = notify()
䞊è¡åŠçïŒåºç€
䞊è¡åŠçãšäžŠååŠçã®æ··å (#55)
èŠçŽ
䞊è¡åŠçãšäžŠååŠçã®åºæ¬çãªéããç解ããããšã¯ã Go éçºè ã«ãšã£ãŠå¿ é ã§ãã䞊è¡åŠçã¯æ§é ã«é¢ãããã®ã§ããã䞊ååŠçã¯å®è¡ã«é¢ãããã®ã§ãã
䞊è¡åŠçãšäžŠååŠçã¯åãã§ã¯ãããŸããã
- 䞊è¡åŠçã¯æ§é ã«é¢ãããã®ã§ããå¥ã ã®äžŠè¡ãŽã«ãŒãã³ãåãçµãããšãã§ããããŸããŸãªæ®µéãå°å ¥ããããšã§ãé次åŠçã䞊è¡åŠçã«å€æŽã§ããŸãã
- 䞊ååŠçã¯å®è¡ã«é¢ãããã®ã§ãã䞊åãŽã«ãŒãã³ãããã«è¿œå ããããšã§ã段éã¬ãã«ã§äžŠååŠçã䜿çšã§ããŸãã
ãŸãšãããšã䞊è¡åŠçã¯ã䞊ååã§ããéšåããã€åé¡ã解決ããããã®æ§é ãæäŸããŸããããªãã¡ã䞊è¡åŠçã«ãã䞊ååŠçãå¯èœ ã«ãªããŸã ã
䞊è¡åŠçã®ã»ããåžžã«æ©ããšèããŠãã (#56)
èŠçŽ
çç·Žããéçºè ã«ãªãã«ã¯ã䞊è¡åŠçãå¿ ãããé«éã§ãããšã¯éããªãããšãèªèããå¿ èŠããããŸããæå°éã®ã¯ãŒã¯ããŒãã®äžŠååŠçã䌎ã解決çã¯ãå¿ ãããé次åŠçããé«éã§ãããšã¯éããŸãããé次åŠçãšäžŠè¡åŠçã®ãã³ãããŒã¯ã¯ãä»®å®ãæ€èšŒããæ¹æ³ã§ããã¹ãã§ãã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
ãã£ãã«ãŸãã¯ãã¥ãŒããã¯ã¹ããã€äœ¿çšããã¹ããã«ã€ããŠæžæã£ãŠãã (#57)
èŠçŽ
ãŽã«ãŒãã³ã®çžäºäœçšãèªèããŠããããšã¯ããã£ãã«ãšãã¥ãŒããã¯ã¹ã®ã©ã¡ããéžæãããã決å®ãããšãã«ã圹ç«ã¡ãŸããäžè¬ã«ã䞊åãŽã«ãŒãã³ã«ã¯åæãå¿ èŠã§ããããããã£ãŠãã¥ãŒããã¯ã¹ãå¿ èŠã§ããå察ã«ã䞊è¡ãŽã«ãŒãã³ã¯éåžžã調æŽãšãªãŒã±ã¹ãã¬ãŒã·ã§ã³ãã€ãŸããã£ãã«ãå¿ èŠãšããŸãã
䞊è¡åŠçã®åé¡ãèæ ®ãããšããã£ãã«ãŸãã¯ãã¥ãŒããã¯ã¹ã䜿çšãã解決çãå®è£ ã§ãããã©ãããå¿ ãããæ確ã§ã¯ãªãå¯èœæ§ããããŸããGoèšèªã¯éä¿¡ã«ããã¡ã¢ãªã®å ±æãä¿é²ãããããèµ·ããããééãã®ãã¡ã®äžã€ã¯ããŠãŒã¹ã±ãŒã¹ã«ãããããããã£ãã«ã®äœ¿çšãåžžã«åŒ·å¶ããããšã§ããããããªããã2 ã€ã®æ¹æ³ã¯è£å®çãªãã®ã§ãããšèŠãªãã¹ãã§ãã
ãã£ãã«ãŸãã¯ãã¥ãŒããã¯ã¹ã¯ã©ã®ãããªå Žåã«äœ¿çšããå¿ èŠãããã®ã§ããããã次ã®å³ã®äŸãããã¯ããŒã³ãšããŠäœ¿çšããŸãããã®äŸã«ã¯ãç¹å®ã®é¢ä¿ãæ〠3 ã€ã®ç°ãªããŽã«ãŒãã³ããããŸãã
- G1 ãš G2 ã¯äžŠåãŽã«ãŒãã³ã§ãããã£ãã«ããã¡ãã»ãŒãžãåä¿¡ãç¶ããåãé¢æ°ãå®è¡ãã 2 ã€ã®ãŽã«ãŒãã³ããããã¯åã HTTP ãã³ãã©ãåæã«å®è¡ãã 2 ã€ã®ãŽã«ãŒãã³ãããããŸããã
- G1 ãš G3 ã¯äžŠè¡ãŽã«ãŒãã³ã§ãããG2 ãš G3 ãåæ§ã§ãããã¹ãŠã®ãŽã«ãŒãã³ã¯å šäœã®äžŠè¡æ§é ã®äžéšã§ãããG1 ãš G2 ãæåã®ã¹ããããå®è¡ããG3 ã次ã®ã¹ããããå®è¡ããŸãã
ååãšããŠã䞊åãŽã«ãŒãã³ã¯ãã¹ã©ã€ã¹ãªã©ã®å ±æãªãœãŒã¹ã«ã¢ã¯ã»ã¹ãããå€æŽãããããå¿ èŠãããå Žåãªã©ã«ã_åæ_ããå¿ èŠããããŸããåæã¯ãã¥ãŒããã¯ã¹ã§ã¯åŒ·å¶ãããŸãããã©ã®ãã£ãã«åã§ã匷å¶ãããŸããïŒãããã¡ãããã£ãã«ãé€ãïŒããããã£ãŠãäžè¬ã«ã䞊åãŽã«ãŒãã³éã®åæã¯ãã¥ãŒããã¯ã¹ãä»ããŠéæãããå¿ èŠããããŸãã
äžæ¹ãäžè¬ã«ã䞊è¡ãŽã«ãŒãã³ã¯ 調æŽããã³ãªãŒã±ã¹ãã¬ãŒã·ã§ã³ ãããå¿ èŠããããŸããããšãã°ãG3 ã G1 ãš G2 ã®äž¡æ¹ããã®çµæãéçŽããå¿ èŠãããå ŽåãG1 ãš G2 ã¯æ°ããäžéçµæãå©çšå¯èœã§ããããšã G3 ã«éç¥ããå¿ èŠããããŸãããã®èª¿æŽã¯ã³ãã¥ãã±ãŒã·ã§ã³ã®ç¯å²ãã€ãŸããã£ãã«ã«è©²åœããŸãã
䞊è¡ãŽã«ãŒãã³ã«é¢ããŠã¯ããªãœãŒã¹ã®æææš©ãããã¹ãããïŒG1 ããã³ G2ïŒããå¥ã®ã¹ãããïŒG3ïŒã«ç§»ç®¡ãããå ŽåããããŸããããšãã°ãG1 ãš G2 ã«ãã£ãŠå ±æãªãœãŒã¹ãè±ãã«ãªã£ãŠããå Žåãããæç¹ã§ãã®ãžã§ãã¯å®äºãããšèŠãªãããŸããããã§ã¯ããã£ãã«ã䜿çšããŠãç¹å®ã®ãªãœãŒã¹ã®æºåãã§ããŠããããšãéç¥ããæææš©ã®ç§»è»¢ãåŠçããå¿ èŠããããŸãã
ãã¥ãŒããã¯ã¹ãšãã£ãã«ã«ã¯ç°ãªãã»ãã³ãã£ã¯ã¹ããããŸããã¹ããŒããå
±æããããšãããŸãã¯å
±æãªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããšãã¯ããã¥ãŒããã¯ã¹ã«ãã£ãŠãã®ãªãœãŒã¹ãžã®æä»çã¢ã¯ã»ã¹ãä¿èšŒãããŸããå察ã«ããã£ãã«ã¯ããŒã¿ã®æç¡ïŒchan struct{}
ã®æç¡ïŒã«é¢ä¿ãªãã·ã°ãã«ãè¡ãä»çµã¿ã§ãã調æŽãæææš©ã®ç§»è»¢ã¯ãã£ãã«ãéããŠè¡ãå¿
èŠããããŸãããŽã«ãŒãã³ã䞊åã䞊è¡ããç¥ãããšãéèŠã§ããäžè¬ã«ã䞊åãŽã«ãŒãã³ã«ã¯ãã¥ãŒããã¯ã¹ãå¿
èŠã§ã䞊è¡ãŽã«ãŒãã³ã«ã¯ãã£ãã«ãå¿
èŠã§ãã
競ååé¡ãç解ããŠããªãïŒããŒã¿ç«¶åãšç«¶åç¶æ ããããŠGoèšèªã®ã¡ã¢ãªã¢ãã«ïŒ (#58)
èŠçŽ
䞊è¡åŠçã«çéãããšããããšã¯ãããŒã¿ç«¶åãšç«¶åç¶æ ãç°ãªãæŠå¿µã§ããããšãç解ããããšãæå³ããŸããããŒã¿ç«¶åã¯ãè€æ°ã®ãŽã«ãŒãã³ãåãã¡ã¢ãªäœçœ®ã«åæã«ã¢ã¯ã»ã¹ãããã®ãã¡ã®å°ãªããšã 1 ã€ãæžã蟌ã¿ãè¡ã£ãŠããå Žåã«çºçããŸããäžæ¹ãããŒã¿ç«¶åããªãããšãå¿ ããã決å®çå®è¡ãæå³ããããã§ã¯ãããŸãããåäœãå¶åŸ¡ã§ããªãã€ãã³ãã®é åºãã¿ã€ãã³ã°ã«äŸåããŠããå Žåãããã¯ç«¶åç¶æ ã§ãã
競ååé¡ã¯ãããã°ã©ããŒãçŽé¢ããå¯èœæ§ã®ãããã°ã®äžã§æãå°é£ãã€æãæœäŒæ§ã®é«ããã°ã® 1 ã€ãšãªããŸããGo éçºè ãšããŠãç§ãã¡ã¯ããŒã¿ç«¶åãšç«¶åç¶æ ãããããåãŒããã圱é¿ãããã³ããããåé¿ããæ¹æ³ãªã©ã®éèŠãªåŽé¢ãç解ããå¿ èŠããããŸãã
ããŒã¿ç«¶å
ããŒã¿ç«¶åã¯ã2 ã€ä»¥äžã®ãŽã«ãŒãã³ãåãã¡ã¢ãªäœçœ®ã«åæã«ã¢ã¯ã»ã¹ããå°ãªããšã 1 ã€ãæžã蟌ã¿ãè¡ã£ãŠããå Žåã«çºçããŸãããã®å Žåãå±éºãªçµæãçããå¯èœæ§ããããŸããããã«æªãããšã«ãç¶æ³ã«ãã£ãŠã¯ãã¡ã¢ãªäœçœ®ã«ç¡æå³ãªãããã®çµã¿åãããå«ãå€ãä¿æãããŠããŸãå¯èœæ§ããããŸãã
ããŸããŸãªææ³ãé§äœ¿ããŠãããŒã¿ç«¶åã®çºçãé²ãããšãã§ããŸããããšãã°
sync/atomic
ããã±ãŒãžã䜿çšãã- 2 ã€ã®ãŽã«ãŒãã³ãåæããéã«ãã¥ãŒããã¯ã¹ã®ãããªç¹å®ã®ç®çã®ããã®ããŒã¿æ§é ãå©çšãã
- ãã£ãã«ã䜿çšã㊠2 ã€ã®ãŽã«ãŒãã³ãéä¿¡ããå€æ°ãäžåºŠã« 1 ã€ã®ãŽã«ãŒãã³ã ãã«ãã£ãŠæŽæ°ãããããã«ãã
競åç¶æ
å®è¡ãããæäœã«å¿ããŠãããŒã¿ç«¶åã®ãªãã¢ããªã±ãŒã·ã§ã³ãå¿ ããã決å®çãªçµæãæå³ããã§ããããããããšã¯ãããŸããã
競åç¶æ ã¯ãåäœãå¶åŸ¡ã§ããªãã€ãã³ãã®ã·ãŒã±ã³ã¹ãŸãã¯ã¿ã€ãã³ã°ã«äŸåããå Žåã«çºçããŸããããã§ã¯ãã€ãã³ãã®ã¿ã€ãã³ã°ããŽã«ãŒãã³ã®å®è¡é åºã§ãã
ãŸãšãããšã䞊è¡åŠçã®ã¢ããªã±ãŒã·ã§ã³ã§äœæ¥ããå ŽåãããŒã¿ç«¶åã¯ç«¶åç¶æ ãšã¯ç°ãªãããšãç解ããããšãäžå¯æ¬ ã§ããããŒã¿ç«¶åã¯ãè€æ°ã®ãŽã«ãŒãã³ãåãã¡ã¢ãªäœçœ®ã«åæã«ã¢ã¯ã»ã¹ãããã®ãã¡ã®å°ãªããšã 1 ã€ãæžã蟌ã¿ãè¡ã£ãŠããå Žåã«çºçããŸããããŒã¿ç«¶åãšã¯ãäºæ³å€ã®åäœãæå³ããŸãããã ããããŒã¿ç«¶åã®ãªãã¢ããªã±ãŒã·ã§ã³ãå¿ ããã決å®çãªçµæãæå³ããããã§ã¯ãããŸãããããŒã¿ç«¶åããªããŠããã¢ããªã±ãŒã·ã§ã³ã¯å¶åŸ¡ãããŠããªãã€ãã³ãïŒãŽã«ãŒãã³ã®å®è¡ããã£ãã«ãžã®ã¡ãã»ãŒãžã®çºä¿¡é床ãããŒã¿ããŒã¹ãžã®åŒã³åºãã®ç¶ç¶æéãªã©ïŒã«äŸåããæåãæã€ããšããããŸãããã®å Žåã¯ç«¶åç¶æ ã§ãã䞊è¡åŠçã®ã¢ããªã±ãŒã·ã§ã³ã®èšèšã«çç·Žããã«ã¯ãäž¡æ¹ã®æŠå¿µãç解ããããšãèèŠã§ãã
ã¯ãŒã¯ããŒãã¿ã€ãããšã®äžŠè¡åŠçã®åœ±é¿ãç解ããŠããªã (#59)
èŠçŽ
äžå®æ°ã®ãŽã«ãŒãã³ãäœæãããšãã¯ãã¯ãŒã¯ããŒãã®ã¿ã€ããèæ
®ããŠãã ãããCPU ããŠã³ãã®ãŽã«ãŒãã³ãäœæãããšããããšã¯ããã®æ°ã GOMAXPROCS
å€æ°ïŒããã©ã«ãã§ã¯ãã¹ãäžã® CPU ã³ã¢ã®æ°ã«åºã¥ãïŒã«è¿ã¥ããããšãæå³ããŸããI/O ããŠã³ãã®ãŽã«ãŒãã³ã®äœæã¯ãå€éšã·ã¹ãã ãªã©ã®ä»ã®èŠå ã«äŸåããŸãã
ããã°ã©ãã³ã°ã§ã¯ãã¯ãŒã¯ããŒãã®å®è¡æéã¯æ¬¡ã®ããããã«ãã£ãŠå¶éãããŸãã
- CPU ã®é床 - ããšãã°ãããŒãžãœãŒãã¢ã«ãŽãªãºã ã®å®è¡ãããã«ããããŸãããã®ã¯ãŒã¯ããŒã㯠CPU ããŠã³ããšåŒã°ããŸãã
- I/O ã®é床 - ããšãã°ãREST åŒã³åºããããŒã¿ããŒã¹ã¯ãšãªã®å®è¡ãããã«ããããŸãããã®ã¯ãŒã¯ããŒã㯠I/O ããŠã³ããšåŒã°ããŸãã
- å©çšå¯èœãªã¡ã¢ãªã®é - ãã®ã¯ãŒã¯ããŒãã¯ã¡ã¢ãªããŠã³ããšåŒã°ããŸãã
è£è¶³
ããæ°å幎ã§ã¡ã¢ãªãéåžžã«å®äŸ¡ã«ãªã£ãããšãèæ ®ãããšã 3 ã€ç®ã¯çŸåšã§ã¯æããŸãã§ãããããã£ãŠããã®ã»ã¯ã·ã§ã³ã§ã¯ãæåã® 2 ã€ã®ã¯ãŒã¯ããŒãã¿ã€ããCPU ããŠã³ããš I/O ããŠã³ãã«çŠç¹ãåœãŠãŸãã
ã¯ãŒã«ãŒã«ãã£ãŠå®è¡ãããã¯ãŒã¯ããŒãã I/O ããŠã³ãã§ããå Žåãå€ã¯äž»ã«å€éšã·ã¹ãã ã«äŸåããŸããéã«ãã¯ãŒã¯ããŒãã CPU ã«äŸåããŠããå ŽåããŽã«ãŒãã³ã®æé©ãªæ°ã¯å©çšå¯èœãª CPU ã³ã¢ã®æ°ã«è¿ããªããŸãïŒãã¹ããã©ã¯ãã£ã¹ã¯ runtime.GOMAXPROCS
ã䜿çšããããšã§ãïŒã䞊è¡åŠçã®ã¢ããªã±ãŒã·ã§ã³ãèšèšããå Žåãã¯ãŒã¯ããŒãã®ã¿ã€ãïŒ I/O ããã㯠CPU ïŒãç¥ãããšãéèŠã§ãã
Go Context ã«å¯Ÿãã誀解 (#60)
èŠçŽ
Go Context ã¯ãGoèšèªã®äžŠè¡åŠçã®åºç€ã®äžéšã§ããããŸãã Context ã䜿çšãããšããããã©ã€ã³ãããŒãšå€ã®ãªã¹ããä¿æã§ããŸãã
https://pkg.go.dev/context
Context ã¯ããããã©ã€ã³ããã£ã³ã»ã«ã·ã°ãã«ããã®ä»ã®å€ã API ã®å¢çãè¶ããŠäŒéããŸãã
ãããã©ã€ã³
ãããã©ã€ã³ãšã¯ã次ã®ããããã§æ±ºå®ãããç¹å®ã®æç¹ãæããŸãã
- çŸåšããã®
time.Duration
ïŒäŸïŒ250 msïŒ time.Time
ïŒäŸïŒ2023-02-07 00:00:00 UTCïŒ
ãããã©ã€ã³ã®ã»ãã³ãã£ã¯ã¹ã¯ããããéããå Žåã¯é²è¡äžã®ã¢ã¯ãã£ããã£ãåæ¢ããå¿ èŠãããããšãäŒããŸããã¢ã¯ãã£ããã£ãšã¯ãããšãã°ããã£ãã«ããã®ã¡ãã»ãŒãžã®åä¿¡ãåŸ æ©ããŠãã I/O ãªã¯ãšã¹ãããŽã«ãŒãã³ã§ãã
ãã£ã³ã»ã«ã·ã°ãã«
Go Context ã®ãã 1 ã€ã®äœ¿çšäŸã¯ããã£ã³ã»ã«ã·ã°ãã«ãäŒéããããšã§ããå¥ã®ãŽã«ãŒãã³å
㧠CreateFileWatcher(ctx context.Context, filename string)
ãåŒã³åºãã¢ããªã±ãŒã·ã§ã³ãäœæããããšãæ³åããŠã¿ãŸãããããã®é¢æ°ã¯ããã¡ã€ã«ããèªã¿åããç¶ããŠæŽæ°ããã£ããããç¹å®ã®ãã¡ã€ã«ãŠã©ããã£ãŒãäœæããŸããæäŸããã Context ãæéåãã«ãªãããã£ã³ã»ã«ããããšããã®é¢æ°ã¯ãããåŠçããŠãã¡ã€ã«èšè¿°åãéããŸãã
Context Value
Go Context ã®æåŸã®äœ¿çšäŸã¯ãããŒãšå€ã®ãªã¹ããéã¶ããšã§ãã Context ã«ããŒãšå€ã®ãªã¹ããå«ããæå³ã¯äœã§ãããããGo Context ã¯æ±çšçã§ããããã䜿çšäŸã¯ç¡éã«ãããŸãã
ããšãã°ããã¬ãŒã¹ã䜿çšããå Žåãç°ãªããµãé¢æ°ã®éã§åãçžé¢ ID ãå ±æãããããšããããããããŸãããäžéšã®éçºè ã¯ããã® ID ãé¢æ°ã·ã°ããã£ã®äžéšã«ããã«ã¯ããŸãã«äŸµç¥çã ãšèãããããããŸããããã®ç¹ã«é¢ããŠãäžãããã Context ã®äžéšãšããŠãããå«ããããšã決å®ããããšãã§ããŸãã
Context ã®ãã£ã³ã»ã«ããã£ãããã
context.Context
ã¿ã€ãã¯ãåä¿¡å°çšã®éç¥ãã£ãã« <-chan struct{}
ãè¿ã Done
ã¡ãœããããšã¯ã¹ããŒãããŸãããã®ãã£ãã«ã¯ã Context ã«é¢é£ä»ããããäœæ¥ããã£ã³ã»ã«ããå¿
èŠãããå Žåã«éããããŸããããšãã°
context.WithCancel
ã§äœæããã Context ã«é¢é£ãã Done ãã£ãã«ã¯ãcancelé¢æ°ãåŒã³åºããããšéããããŸããcontext.WithDeadline
ã§äœæãã Context ã«é¢é£ãã Done ãã£ãã«ã¯ããããã©ã€ã³ãéãããšéããããŸãã
泚æãã¹ãç¹ã® 1 ã€ã¯ãå éšãã£ãã«ã¯ãç¹å®ã®å€ãåãåã£ããšãã§ã¯ãªãã Context ããã£ã³ã»ã«ããããšãããŸãã¯ãããã©ã€ã³ã«éãããšãã«éããå¿ èŠããããšããããšã§ãããã£ãã«ã®ã¯ããŒãºã¯ããã¹ãŠã®æ¶è²»è ãŽã«ãŒãã³ãåãåãå¯äžã®ãã£ãã«ã¢ã¯ã·ã§ã³ã§ããããã§ãããã®ããã«ããŠã Context ããã£ã³ã»ã«ããããããããã©ã€ã³ã«éãããšããã¹ãŠã®æ¶è²»è ã«éç¥ãå±ããŸãã
ãŸãšãããšãçç·Žãã Go éçºè ã«ãªãã«ã¯ã Context ãšãã®äœ¿çšæ¹æ³ã«ã€ããŠç解ããå¿ èŠããããŸããååãšããŠããŠãŒã¶ãŒãåŸ æ©ãããããé¢æ°ã¯ Context ãååŸããã¹ãã§ããããã«ãããäžæµã®åŒã³åºãå ããã®é¢æ°ããã€åŒã³åºããã決å®ã§ããããã«ãªãããã§ãã
䞊è¡åŠçïŒå®è·µ
äžé©å㪠Context ãåºããŠããŸã (#61)
èŠçŽ
Context ãäŒæããéã«ã¯ãContext ããã£ã³ã»ã«ã§ããæ¡ä»¶ãç解ããããšãéèŠã§ããããšãã°ãã¬ã¹ãã³ã¹ãéä¿¡ãããéã« HTTP ãã³ãã©ã Context ããã£ã³ã»ã«ãããšããªã©ã§ãã
å€ãã®ç¶æ³ã§ã¯ãGo Context ãäŒæããããšãæšå¥šãããŸãããã ããContext ã®äŒæã«ãã£ãŠè»œåŸ®ãªãã°ãçºçãããµãé¢æ°ãæ£ããå®è¡ãããªããªãå ŽåããããŸãã
次ã®äŸãèããŠã¿ãŸããããããã€ãã®ã¿ã¹ã¯ãå®è¡ããŠã¬ã¹ãã³ã¹ãè¿ã HTTP ãã³ãã©ãå
¬éããŸãããã ããã¬ã¹ãã³ã¹ãè¿ãçŽåã«ãããã Kafka ãããã¯ã«éä¿¡ããããšæã£ãŠããŸããHTTP ã³ã³ã·ã¥ãŒãã«ã¬ã€ãã³ã·ã®ç¹ã§ããã«ãã£ã課ããããªãã®ã§ãpublish ã¢ã¯ã·ã§ã³ãæ°ãããŽã«ãŒãã³å
ã§éåæã«åŠçããããšèããŠããŸããããšãã°ãContext ããã£ã³ã»ã«ãããå Žåã«ã¡ãã»ãŒãžã® publish ã¢ã¯ã·ã§ã³ãäžæã§ããããã«ãContext ãåãå
¥ãã publish
é¢æ°ãèªç±ã«äœ¿ãããšããŸããå¯èœãªå®è£
ã¯æ¬¡ã®ãšããã§ãã
func handler(w http.ResponseWriter, r *http.Request) {
response, err := doSomeTask(r.Context(), r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
err := publish(r.Context(), response)
// err ã®åŠçããã
}()
writeResponse(response)
}
ãã®ã³ãŒãã®äœãåé¡ãªã®ã§ãããããHTTP ãªã¯ãšã¹ãã«ä»ããã Context ã¯ãããŸããŸãªç¶æ³ã§ãã£ã³ã»ã«ãããå¯èœæ§ãããããšãç¥ã£ãŠããå¿ èŠããããŸãã
- ã¯ã©ã€ã¢ã³ãã®æ¥ç¶ãçµäºãããšã
- HTTP/2ãªã¯ãšã¹ãã®å Žåããªã¯ãšã¹ãããã£ã³ã»ã«ããããšã
- ã¯ã©ã€ã¢ã³ãã«ã¬ã¹ãã³ã¹ãæžãæ»ããããšã
æåã® 2 ã€ã®ã±ãŒã¹ã§ã¯ãåŠçã¯ããããæ£ããè¡ãããŸããããšãã°ãdoSomeTask ããã¬ã¹ãã³ã¹ãåãåã£ããã®ã®ãã¯ã©ã€ã¢ã³ããæ¥ç¶ãéããå Žåãã¡ãã»ãŒãžã publish ãããªãããã«ãContext ãæ¢ã«ãã£ã³ã»ã«ãããç¶æ 㧠publish ãåŒã³åºããŠãåé¡ã¯ããããèµ·ããŸãããããããæåŸã®ã±ãŒã¹ã¯ã©ãã§ããããã
ã¬ã¹ãã³ã¹ãã¯ã©ã€ã¢ã³ãã«æžã蟌ãŸãããšãèŠæ±ã«é¢é£ä»ãããã Context ããã£ã³ã»ã«ãããŸãããããã£ãŠã競åç¶æ ã«çŽé¢ããŸãã
- ã¬ã¹ãã³ã¹ã Kafka ã® publish åŸã«æžãããå Žåãã¬ã¹ãã³ã¹ãè¿ããã¡ãã»ãŒãžãæ£åžžã«å ¬éããŸãã
- ãã ããKafka ã® publish åãŸã㯠publish äžã«ã¬ã¹ãã³ã¹ãæžãããå Žåãã¡ãã»ãŒãžã¯ publish ãããã¹ãã§ã¯ãããŸããã
åŸè ã®å ŽåãHTTP ã¬ã¹ãã³ã¹ãããã«è¿ãã®ã§ãpublish ãåŒã³åºããšãšã©ãŒãè¿ãããŸãã
è£è¶³
Go 1.21 ããã¯ããã£ã³ã»ã«ããã«æ°ãã Context ãäœæããæ¹æ³ãè¿œå ãããŸããã context.WithoutCancel
ã¯ã芪ããã£ã³ã»ã«ããããšãã«ãã£ã³ã»ã«ãããŠããªã芪ã®ã³ããŒãè¿ããŸãã
ãŸãšãããšãContext ã®äŒæã¯æ éã«è¡ãå¿ èŠããããŸãã
åæ¢ãã¹ããšããç¥ããã«ãŽã«ãŒãã³ãéå§ããŠããŸã (#62)
èŠçŽ
ãªãŒã¯ãé¿ããããšã¯ããŽã«ãŒãã³ãéå§ããããã³ã«ãæçµçã«åæ¢ããå¿ èŠãããããšãæå³ããŸãã
ãŽã«ãŒãã³ã¯ç°¡åã«è¡ãããšãã§ããŸããéåžžã«ç°¡åã§ãããããæ°ãããŽã«ãŒãã³ããã€åæ¢ãããã«ã€ããŠã®èšç»ãå¿ ãããç«ãŠãŠããªãå¯èœæ§ãããããªãŒã¯ã«ã€ãªããããšããããŸãããŽã«ãŒãã³ããã€åæ¢ããã°ãããããããªãã®ã¯ãGoèšèªã§ããããèšèšäžã®åé¡ã§ããã䞊è¡åŠçã«ããããã¹ã§ãã
å ·äœçãªäŸã«ã€ããŠèª¬æããŸããããå€éšèšå®ïŒããŒã¿ããŒã¹æ¥ç¶ãªã©ã䜿çšãããã®ãªã©ïŒãç£èŠããå¿ èŠãããã¢ããªã±ãŒã·ã§ã³ãèšèšããŸãããŸãã次ã®ãããªå®è£ ãããŠã¿ãŸãã
func main() {
newWatcher()
// ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãã
}
type watcher struct { /* ããã€ãã®ãªãœãŒã¹ */ }
func newWatcher() {
w := watcher{}
go w.watch() // å€éšèšå®ãç£èŠãããŽã«ãŒãã³ãäœæãã
}
ãã®ã³ãŒãã®åé¡ã¯ãã¡ã€ã³ã®ãŽã«ãŒãã³ãçµäºãããšïŒãããã OS ã·ã°ãã«ãŸãã¯æéã®ã¯ãŒã¯ããŒãã®ããïŒãã¢ããªã±ãŒã·ã§ã³ãåæ¢ããŠããŸãããšã§ãããããã£ãŠããŠã©ããã£ãŒã«ãã£ãŠäœæããããªãœãŒã¹ã¯æ£åžžã«éããããŸããããããé²ãã«ã¯ã©ãããã°ããã§ããããã
1 ã€ã®æ¹æ³ãšããŠã¯ãmain ãæ»ã£ããšãã«ãã£ã³ã»ã«ããã Context ã newWatcher ã«æž¡ãããšãæããããŸãã
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newWatcher(ctx)
// ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãã
}
func newWatcher(ctx context.Context) {
w := watcher{}
go w.watch(ctx)
}
äœæãã Context ã watch ã¡ãœããã«äŒæããŸããContext ããã£ã³ã»ã«ããããšããŠã©ããã£ãŒæ§é äœã¯ãã®ãªãœãŒã¹ãéããŸããããããwatch ããããè¡ãæéã確å®ã«ãããšã¯ãããŸãããããã¯èšèšäžã®æ¬ é¥ã§ãã
åé¡ã¯ããŽã«ãŒãã³ãåæ¢ããå¿ èŠãããããšãäŒããããã«ã·ã°ãã«ã䜿çšããããšã§ãããªãœãŒã¹ãéãããããŸã§ã芪ã®ãŽã«ãŒãã³ããããã¯ããŸããã§ããããããªããªãããã«ããŸãããã
func main() {
w := newWatcher()
defer w.close()
// ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãã
}
func newWatcher() watcher {
w := watcher{}
go w.watch()
return w
}
func (w watcher) close() {
// ãªãœãŒã¹ãéãã
}
ãªãœãŒã¹ãéããæéã«ãªã£ãããšã watcher
ã«éç¥ãã代ããã«ã defer
ã䜿çšããŠãã®ãclose
ã¡ãœãããåŒã³åºããã¢ããªã±ãŒã·ã§ã³ãçµäºããåã«ãªãœãŒã¹ã確å®ã«éããããããã«ããŸãã
ãŸãšãããšããŽã«ãŒãã³ã¯ä»ã®ãªãœãŒã¹ãšåæ§ãã¡ã¢ãªãä»ã®ãªãœãŒã¹ã解æŸããããã«æçµçã«éããå¿ èŠãããããšã«æ³šæããŠãã ããããŽã«ãŒãã³ããã€åæ¢ããããç¥ããã«éå§ããã®ã¯èšèšäžã®åé¡ã§ãããŽã«ãŒãã³ãéå§ããããšãã¯åžžã«ããã€åæ¢ãããã«ã€ããŠæ確ãªèšç»ãç«ãŠãå¿ èŠããããŸããæåŸã«ãªããŸãããããŽã«ãŒãã³ããªãœãŒã¹ãäœæãããã®æå¹æéãã¢ããªã±ãŒã·ã§ã³ã®åç¶æéã«ãã€ã³ããããŠããå Žåã¯ãã¢ããªã±ãŒã·ã§ã³ãçµäºããåã«ãã®ãŽã«ãŒãã³ãå®äºããã®ãåŸ ã£ãæ¹ããããã確å®ã§ããããããããšã§ããªãœãŒã¹ãééããªã解æŸã§ããŸãã
ãŽã«ãŒãã³ãšã«ãŒãå€æ°ã«æ³šæããªã (#63)
泚æ
ãã®ãã¹ã¯ Goã1.22 ããã¯æ°ã«ããå¿ èŠããããŸããïŒè©³çŽ°ïŒã
select
ãšãã£ãã«ã䜿çšããŠæ±ºå®çåäœãæåŸ
ãã (#64)
èŠçŽ
è€æ°ã®ãªãã·ã§ã³ãå¯èœãªå Žåãè€æ°ã®ãã£ãã«ã§ select
ãããšã±ãŒã¹ãã©ã³ãã ã«éžæãããããšãç解ãããšã䞊è¡åŠçã«ããã軜埮ãªãã°ã«ã€ãªããå¯èœæ§ã®ãã誀ã£ãä»®å®ãç«ãŠãããšããªããªããŸãã
Go éçºè ããã£ãã«ãæäœãããšãã«ãããã¡ãªééãã® 1 ã€ã¯ãselect ãè€æ°ã®ãã£ãã«ã§ã©ã®ããã«åäœãããã«ã€ããŠèª€ã£ãç解ãããããšã§ãã
ããšãã°ã次ã®å ŽåãèããŠã¿ãŸãããïŒ disconnectCh
ã¯ãããã¡ãªããã£ãã«ã§ãïŒã
go func() {
for i := 0; i < 10; i++ {
messageCh <- i
}
disconnectCh <- struct{}{}
}()
for {
select {
case v := <-messageCh:
fmt.Println(v)
case <-disconnectCh:
fmt.Println("disconnection, return")
return
}
}
ãã®äŸãè€æ°åå®è¡ããå Žåãçµæã¯ã©ã³ãã ã«ãªããŸãã
ã©ãããããã 10 éã®ã¡ãã»ãŒãžãæ¶è²»ããã®ã§ã¯ãªãããã®ãã¡ã®æ°éã ããåä¿¡ããŸãããããã¯ãè€æ°ã®ãã£ãã«ãšäœµçšããå Žåã® select æã®ä»æ§ã«ãããã®ã§ãïŒhttps:// go.dev/ref/specïŒã
Quote
1 ã€ä»¥äžã®éä¿¡ãç¶è¡ã§ããå Žåãåäžã®æ¬äŒŒã©ã³ãã éžæã«ãã£ãŠãç¶è¡ã§ãã 1 ã€ã®éä¿¡ãéžæãããŸãã
æåã«äžèŽããã±ãŒã¹ãåªå ããã switch æãšã¯ç°ãªããselect æã¯è€æ°ã®ãªãã·ã§ã³ãå¯èœãªå Žåã«ã©ã³ãã ã«éžæããŸãã
ãã®åäœã¯æåã¯å¥åŠã«æãããããããŸãããããããããã¯ã¹ã¿ããŒã·ã§ã³ãé²ããšããçç±ããã£ãŠã®ããšã§ããæåã«éžæãããéä¿¡ããœãŒã¹ã®é åºã«åºã¥ããŠãããšããŸãããã®å Žåãéä¿¡é床ãéãããã«ãããšãã° 1 ã€ã®ãã£ãã«ããããåä¿¡ã§ããªããšããç¶æ³ã«é¥ãå¯èœæ§ããããŸãããããé²ãããã«ãGoèšèªã®èšèšè ã¯ã©ã³ãã éžæã䜿çšããããšã«ããŸããã
è€æ°ã®ãã£ãã«ã§ select
ã䜿çšããå Žåãè€æ°ã®ãªãã·ã§ã³ããããªãããœãŒã¹é åºã®æåã®ã±ãŒã¹ãèªåçã«åªå
ãããããã§ã¯ãªãããšã«æ³šæããå¿
èŠããããŸãã代ããã«ãGoèšèªã¯ã©ã³ãã ã«éžæãããããã©ã®ãªãã·ã§ã³ãéžæããããã¯ä¿èšŒãããŸããããã®åäœãå
æããã«ã¯ãåäžã®çç£è
ãŽã«ãŒãã³ã®å Žåããããã¡ãªãã®ãã£ãã«ãŸãã¯åäžã®ãã£ãã«ã䜿çšããããšãã§ããŸãã
éç¥ãã£ãã«ã䜿çšããŠããªã (#65)
èŠçŽ
chan struct{}
åã䜿çšããŠéç¥ãéä¿¡ããŸãããã
ãã£ãã«ã¯ãã·ã°ãã«ãä»ããŠãŽã«ãŒãã³éã§éä¿¡ããããã®ã¡ã«ããºã ã§ããã·ã°ãã«ã«ã¯ããŒã¿ãå«ãŸããŠãããã©ããã¯é¢ä¿ãããŸããã
å
·äœçãªäŸãèŠãŠã¿ãŸããããéä¿¡ã®åæãçºçãããã³ã«ãããéç¥ãããã£ãã«ãäœæããŸãã 1 ã€ã®æ¹æ³ãšããŠãããã chan bool
ãšããŠæ±ãããšãæããããŸãã
ããã§ããã®ãããªãã£ãã«ãæäŸãã API ãšå¯Ÿè©±ãããšããŸããããã¯çåœå€ã®ãã£ãã«ã§ãããããtrue
ãŸã㯠false
ã®ã¡ãã»ãŒãžãåä¿¡ã§ããŸããtrue
ãäœãäŒããŠãããã¯ããããæããã§ãããããããã false
ãšã¯äœãæå³ããã®ã§ãããããéä¿¡ãåæãããŠããªããšããããšã§ããããããã®å Žåãã©ããããã®é »åºŠã§ãã®ãããªã·ã°ãã«ãåä¿¡ããã®ã§ããããããããã¯åæ¥ç¶ãããšããããšã§ããããããããã false
ãåãåãããšãæåŸ
ãã¹ããªã®ã§ããããããããã true
ã¡ãã»ãŒãžãåãåãããšã ããæåŸ
ãã¹ãã§ãããã
ãã®å Žåãæ
å ±ãäŒããããã«ç¹å®ã®å€ã¯å¿
èŠãªãããšãæå³ããããŒã¿ã® ãªã ãã£ãã«ãå¿
èŠã«ãªããŸãããããåŠçããæ
£çšçãªæ¹æ³ã¯ã空ã®æ§é äœã®ãã£ãã«ââ chan struct{}
ââã䜿çšããããšã§ãã
nil ãã£ãã«ã䜿çšããŠããªã (#66)
èŠçŽ
nil ãã£ãã«ã䜿çšããããšã«ãã£ãŠãããšãã° select
æããã±ãŒã¹ã åé€ ã§ããããã䞊è¡åŠçãè¡ãéã®éå
·ã®äžã€ãšããŠäœ¿ããããã«ãªãã¹ãã§ãã
次ã®ã³ãŒãã«ãã£ãŠäœãè¡ãããã§ããããã
ch
㯠chan int
åã§ãããã£ãã«ã®ãŒãå€ã¯ nil ã§ããã®ã§ã ch
㯠nil
ã§ãããŽã«ãŒãã³ã¯ panic ãèµ·ãããŸããããã ããæ°žä¹
ã«ãããã¯ããŸãã
nil ãã£ãã«ã«ã¡ãã»ãŒãžãéä¿¡ããå Žåãåçã¯åãã§ãã以äžã®ãŽã«ãŒãã³ã¯æ°žä¹ ã«ãããã¯ããŸãã
ã§ã¯ãGoèšèªã nil ãã£ãã«ãšã®éã§ã¡ãã»ãŒãžã®éåä¿¡ãèš±å¯ããç®çã¯äœã§ãããããããšãã°ã2 ã€ã®ãã£ãã«ãããŒãžããæ £çšçãªæ¹æ³ãå®è£ ããã®ã«ã nil ãã£ãã«ã䜿çšããããšãã§ããŸãã
func merge(ch1, ch2 <-chan int) <-chan int {
ch := make(chan int, 1)
go func() {
for ch1 != nil || ch2 != nil { // æäœã§ãäžã€ã®ãã£ãã«ã nil ã§ãªããã°ç¶è¡ãã
select {
case v, open := <-ch1:
if !open {
ch1 = nil // éããã ch1 ã nil ãã£ãã«ã«å²ãåœãŠã
break
}
ch <- v
case v, open := <-ch2:
if !open {
ch2 = nil // éããã ch2 ã nil ãã£ãã«ã«å²ãåœãŠã
break
}
ch <- v
}
}
close(ch)
}()
return ch
}
ãã®æŽç·Žããã解決çã¯ãnil ãã£ãã«ãå©çšããŠãäœããã®æ¹æ³ã§ select
æãã 1 ã€ã®ã±ãŒã¹ã åé€ ããŸãã
nil ãã£ãã«ã¯ç¶æ³ã«ãã£ãŠã¯äŸ¿å©ã§ãããGo éçºè ã¯äžŠè¡åŠçãæ±ãéã«äœ¿ãããªããããã«ãªã£ãŠããã¹ãã§ãã
ãã£ãã«ã®å®¹éã«ã€ããŠå°æããŠãã (#67)
èŠçŽ
åé¡ãçºçããå Žåã¯ã䜿çšãããã£ãã«ã®åãæ éã«æ±ºå®ããŠãã ãããåæã匷åã«ä¿èšŒããŠãããã®ã¯ãããã¡ãªããã£ãã«ã®ã¿ã§ãã
ãããã¡ãããã£ãã«ä»¥å€ã®ãã£ãã«ã®å®¹éãæå®ããã«ã¯æ£åœãªçç±ãããã¹ãã§ãã
æååãã©ãŒãããã§èµ·ããåŸãå¯äœçšãå¿ããŠããŸãïŒ etcd ããŒã¿ç«¶åã®äŸãšãããããã¯ïŒ (#68)
èŠçŽ
æååã®æžåŒèšå®ãæ¢åã®é¢æ°ãåŒã³åºãå¯èœæ§ãããããšãèªèããããšã¯ããããããã¯ããã®ä»ã®ããŒã¿ç«¶åã®å¯èœæ§ã«æ³šæããããšãæå³ããŸãã
append
ã§ããŒã¿ç«¶åãèµ·ãããŠããŸã (#69)
èŠçŽ
append
ã®åŒã³åºãã¯å¿
ãããããŒã¿ç«¶åããªãããã§ã¯ãããŸãããããã«ãå
±æã¹ã©ã€ã¹äžã§åæã«äœ¿çšããŠã¯ãããŸããã
ã¹ã©ã€ã¹ãšãããã§ãã¥ãŒããã¯ã¹ãæ£ãã䜿çšããŠããªã (#70)
èŠçŽ
ã¹ã©ã€ã¹ãšãããã¯ãã€ã³ã¿ã§ããããšãèŠããŠãããšãå žåçãªããŒã¿ç«¶åãé²ãããšãã§ããŸãã
sync.WaitGroup
ãæ£ãã䜿çšããŠããªã (#71)
èŠçŽ
sync.WaitGroup
ãæ£ãã䜿çšããã«ã¯ããŽã«ãŒãã³ãèµ·åããåã« Add
ã¡ãœãããåŒã³åºããŸãããã
sync.Cond
ã«ã€ããŠå¿ããŠããŸã (#72)
èŠçŽ
sync.Cond
ã䜿çšãããšãè€æ°ã®ãŽã«ãŒãã³ã«ç¹°ãè¿ãéç¥ãéä¿¡ã§ããŸãã
errgroup
ã䜿çšããŠããªã (#73)
èŠçŽ
errgroup
ããã±ãŒãžã䜿çšããŠããŽã«ãŒãã³ã®ã°ã«ãŒããåæãããšã©ãŒãš Context ãåŠçã§ããŸãã
sync
åã®ã³ã㌠(#74)
èŠçŽ
sync
åã¯ã³ããŒãããã¹ãã§ã¯ãããŸããã
æšæºã©ã€ãã©ãª
ééã£ãæéãæå®ãã (#75)
èŠçŽ
time.Duration
ãåãå
¥ããé¢æ°ã«ã¯æ³šæãæã£ãŠãã ãããæŽæ°ãæž¡ãããšã¯èš±å¯ãããŠããŸãããæ··ä¹±ãæããªãããã« time API ã䜿çšããããåªããŠãã ããã
æšæºã©ã€ãã©ãªã®å€ãã®é¢æ°ã¯ãint64
åã®ãšã€ãªã¢ã¹ã§ãã time.Duration
ãåãå
¥ããŸãããã ãã1 åäœã® time.Duration
ã¯ãä»ã®ããã°ã©ãã³ã°èšèªã§äžè¬çã«èŠããã 1 ããªç§ã§ã¯ãªãã1 ããç§ãè¡šããŸãããã®çµæãtime.Duration
API ã䜿çšãã代ããã«æ°å€åãæž¡ããšãäºæ³å€ã®åäœãçºçããå¯èœæ§ããããŸãã
ä»èšèªã䜿çšããããšã®ããéçºè
ã®æ¹ã¯ã次ã®ã³ãŒãã«ãã£ãŠ 1 ç§åšæã®æ°ãã time.Ticker
ãçæããããšèãããããããŸããã
ããããªããã1,000 time.Duration
= 1,000 ããç§ã§ãããããæ³å®ãããŠãã 1ç§ ã§ã¯ãªãã1,000 ããç§ = 1 ãã€ã¯ãç§ã®åšæã«ãªããŸãã
æ··ä¹±ãäºæ³å€ã®åäœãæããªãããããã€ã time.Duration
API ã䜿çšããã¹ãã§ãã
ticker = time.NewTicker(time.Microsecond)
// ãããã¯
ticker = time.NewTicker(1000 * time.Nanosecond)
time.After
ãšã¡ã¢ãªãªãŒã¯ (#76)
èŠçŽ
ç¹°ãè¿ãããé¢æ°ïŒã«ãŒãã HTTP ãã³ãã©ãªã©ïŒã§ time.After
ã®åŒã³åºããåé¿ãããšãããŒã¯æã®ã¡ã¢ãªæ¶è²»ãåé¿ã§ããŸããtime.After
ã«ãã£ãŠçæããããªãœãŒã¹ã¯ã timer ãçµäºãããšãã«ã®ã¿è§£æŸãããŸãã
JSON åŠçã§ãããã¡ãªééã (#77)
- åã®åã蟌ã¿ã«ããäºæ³å€ã®åäœ
Go æ§é äœã§åã蟌ã¿ãã£ãŒã«ãã䜿çšããå Žåã¯æ³šæããŠãã ããã ãªããªã json.Marshaler
ã€ã³ã¿ãã§ãŒã¹ãå®è£
ãã time.Time åã蟌ã¿ãã£ãŒã«ãã®ãããªãã£ãããªãã°ãçºçããŠãããã©ã«ãã®ããŒã·ã£ãªã³ã°åäœããªãŒããŒã©ã€ããããå¯èœæ§ãããããã§ãã
- JSON ãš monotonic clock
2 ã€ã® time.Time
æ§é äœãæ¯èŒããå Žåãtime.Time
ã«ã¯ wall clock ãš monotonic clock ã®äž¡æ¹ãå«ãŸããŠããã== æŒç®åã䜿çšããæ¯èŒã¯äž¡æ¹ã® clock ã«å¯ŸããŠè¡ãããããšãæãåºããŠãã ããã
any
ã®ããã
JSON ããŒã¿ã®ã¢ã³ããŒã·ã£ãªã³ã°äžã«ããããæäŸãããšãã«ééããé¿ããããã«ãæ°å€ã¯ããã©ã«ã㧠float64
ã«å€æãããããšã«æ³šæããŠãã ããã
SQL ã§ãããã¡ãªééã (#78)
sql.Open
ãå¿ ãããããŒã¿ããŒã¹ãžã®æ¥ç¶ã確ç«ããããã§ã¯ãªãããšãå¿ããŠãã
èšå®ãè©ŠããããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ã§ããããšã確èªããå¿
èŠãããå Žåã¯ã Ping
ãŸã㯠PingContext
ã¡ãœãããåŒã³åºããŸãããã
- ã³ãã¯ã·ã§ã³ããŒãªã³ã°ã®ããšãå¿ãã
å®éçšæ°Žæºã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãããŒã¿ããŒã¹æ¥ç¶ãã©ã¡ãŒã¿ãèšå®ããŸãããã
- ããªãã¢ãã¹ããŒãã¡ã³ãã䜿çšããŠããªã
SQL ã®ããªãã¢ãã¹ããŒãã¡ã³ãã䜿çšãããšãã¯ãšãªãããå¹ççãã€ç¢ºå®ã«ãªããŸãã
- null å€ã誀ã£ãæ¹æ³ã§åŠçããŠãã
ããŒãã«å
ã® null ã蚱容ãããŠããåã¯ããã€ã³ã¿ãŸã㯠sql.NullXXX
åã䜿çšããŠåŠçããŸãããã
- è¡ã®å埩åŠçã«ãããšã©ãŒãåŠçããªã
è¡ã®å埩åŠçã®åŸã« sql.Rows
ã® Err
ã¡ãœãããåŒã³åºããŠã次ã®è¡ã®æºåäžã«ãšã©ãŒãèŠéããŠããªãããšã確èªããŸãããã
äžæçãªãªãœãŒã¹ïŒ HTTP bodyãsql.Rows
ãããã³ os.File
ïŒãéããŠããªã (#79)
èŠçŽ
ãªãŒã¯ãé¿ããããã«ã io.Closer
ãå®è£
ããŠãããã¹ãŠã®æ§é äœãæåŸã«ã¯éããŸãããã
HTTP ãªã¯ãšã¹ãã«å¿çããåŸã® return æãå¿ããŠããŸã (#80)
èŠçŽ
HTTP ãã³ãã©ã®å®è£
ã§ã®äºæ³å€ã®åäœãé¿ãããããhttp.Error
ã®åŸã«ãã³ãã©ãåæ¢ãããå Žåã¯ãreturn
æãå¿ããªãããã«ããŠãã ããã
æšæºã® HTTP ã¯ã©ã€ã¢ã³ããšãµãŒããŒã䜿çšããŠãã (#81)
èŠçŽ
å®éçšæ°Žæºã®ã¢ããªã±ãŒã·ã§ã³ãæ±ããŠããå Žåã¯ãæšæºã® HTTP ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®å®è£ ã䜿çšããªãã§ãã ããããããã®å®è£ ã«ã¯ãã¿ã€ã ã¢ãŠãã皌åç°å¢ã§å¿ é ã§ããã¹ãåäœãæ¬ èœããŠããŸãã
ãã¹ã
ãã¹ããåé¡ããŠããªãïŒãã«ãã¿ã°ãç°å¢å€æ°ãã·ã§ãŒãã¢ãŒãïŒ (#82)
èŠçŽ
ãã«ããã©ã°ãç°å¢å€æ°ããŸãã¯ã·ã§ãŒãã¢ãŒãã䜿çšããŠãã¹ããåé¡ãããšããã¹ãããã»ã¹ãããå¹ççã«ãªããŸãããã«ããã©ã°ãŸãã¯ç°å¢å€æ°ã䜿çšããŠãã¹ãã«ããŽãªïŒããšãã°ãåäœãã¹ããšçµ±åãã¹ãïŒãäœæããçæéã®ãã¹ããšé·æéã®ãã¹ããåºå¥ããããšã§ãå®è¡ãããã¹ãã®çš®é¡ã決å®ã§ããŸãã
-race
ãã©ã°ãæå¹ã«ããŠããªã (#83)
èŠçŽ
䞊è¡ã¢ããªã±ãŒã·ã§ã³ãäœæããå Žåã¯ã -race
ãã©ã°ãæå¹ã«ããããšã匷ããå§ãããŸããããããããšã§ããœãããŠã§ã¢ã®ãã°ã«ã€ãªããå¯èœæ§ã®ããæœåšçãªããŒã¿ç«¶åãçºèŠã§ããããã«ãªããŸãã
ãã¹ãå®è¡ã¢ãŒãïŒ -parallel
ããã³ -shuffle
ïŒã䜿çšããŠããªã (#84)
èŠçŽ
-parallel
ãã©ã°ã䜿çšããã®ã¯ãç¹ã«é·æéå®è¡ããããã¹ããé«éåããã®ã«å¹æçã§ãã -shuffle
ãã©ã°ã䜿çšãããšããã¹ãã¹ã€ãŒãããã°ãé ãå¯èœæ§ã®ããééã£ãä»®å®ã«äŸåããªãããã«ããããšãã§ããŸãã
ããŒãã«é§åãã¹ãã䜿çšããªã (#85)
èŠçŽ
ããŒãã«é§åãã¹ãã¯ãã³ãŒãã®éè€ãé²ããå°æ¥ã®æŽæ°ã®åŠçã容æã«ããããã«ãäžé£ã®é¡äŒŒãããã¹ããã°ã«ãŒãåããå¹ççãªæ¹æ³ã§ãã
åäœãã¹ãã§ã®ã¹ãªãŒã (#86)
èŠçŽ
ãã¹ãã®äžå®å®ãããªãããããå ç¢ã«ããããã«ãåæã䜿çšããŠã¹ãªãŒããåé¿ããŸããããåæãäžå¯èœãªå Žåã¯ããªãã©ã€ææ³ãæ€èšããŠãã ããã
time API ãå¹ççã«åŠçã§ããŠããªã (#87)
èŠçŽ
time API ã䜿çšããŠé¢æ°ãåŠçããæ¹æ³ãç解ããããšã§ããã¹ãã®äžå®å®ãã軜æžããããšãã§ããŸããé ããäŸåé¢ä¿ã®äžéšãšã㊠time ãåŠçããããã¯ã©ã€ã¢ã³ãã« time ãæäŸããããã«èŠæ±ããããããªã©ãæšæºçãªæ段ãå©çšã§ããŸãã
ãã¹ãã«é¢ãããŠãŒãã£ãªãã£ããã±ãŒãžïŒ httptest
ããã³ iotest
ïŒã䜿çšããŠããªã (#88)
httptest
ããã±ãŒãžã¯ãHTTP ã¢ããªã±ãŒã·ã§ã³ãæ±ãã®ã«åœ¹ç«ã¡ãŸããã¯ã©ã€ã¢ã³ããšãµãŒããŒã®äž¡æ¹ããã¹ãããããã®äžé£ã®ãŠãŒãã£ãªãã£ãæäŸããŸãã
iotest
ããã±ãŒãžã¯ãio.Reader ãäœæããã¢ããªã±ãŒã·ã§ã³ã®ãšã©ãŒèæ§ããã¹ãããã®ã«åœ¹ç«ã¡ãŸãã
äžæ£ç¢ºãªãã³ãããŒã¯ã®äœæ (#89)
èŠçŽ
ãã³ãããŒã¯ã«ã€ããŠ
- ãã³ãããŒã¯ã®ç²ŸåºŠãç¶æããã«ã¯ãtime ã¡ãœããã䜿çšããŸãããã
- ãã³ãã¿ã€ã ãå¢ããããbenchstat ãªã©ã®ããŒã«ã䜿çšããããšã§ããã€ã¯ããã³ãããŒã¯ãæ±ãããããªããŸãã
- ã¢ããªã±ãŒã·ã§ã³ãæçµçã«å®è¡ããã·ã¹ãã ããã€ã¯ããã³ãããŒã¯ãå®è¡ããã·ã¹ãã ãšç°ãªãå Žåã¯ããã€ã¯ããã³ãããŒã¯ã®çµæã«æ³šæããŠãã ããã
- ã³ã³ãã€ã©ã®æé©åã«ãã£ãŠãã³ãããŒã¯ã®çµæã誀éåãããªãããããã¹ã察象ã®é¢æ°ãå¯äœçšãåŒãèµ·ããããã«ããŠãã ããã
- ãªãã¶ãŒããŒå¹æãé²ãã«ã¯ãCPU ã«äŸåããé¢æ°ã䜿çšããããŒã¿ããã³ãããŒã¯ãåçæãããã匷å¶ããŠãã ããã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
Goèšèªã®ãã¹ãæ©èœããã¹ãŠè©ŠããŠããªã (#90)
- ã³ãŒãã«ãã¬ããž
ã³ãŒãã®ã©ã®éšåã«æ³šæãå¿
èŠããããã«ç¢ºèªããããã«ã-coverprofile
ãã©ã°ãæå®ããŠã³ãŒãã«ãã¬ããžã䜿çšããŸãããã
- å¥ã®ããã±ãŒãžããã®ãã¹ã
å éšã§ã¯ãªãå ¬éãããåäœã«çŠç¹ãåœãŠããã¹ãã®äœæã匷å¶ããããã«ãåäœãã¹ãã¯å¥ã ã®ããã±ãŒãžã«é 眮ããŸãããã
- ãŠãŒãã£ãªãã£é¢æ°
åŸæ¥ã® if err != nil
ã®ä»£ããã« *testing.T
å€æ°ã䜿çšããŠãšã©ãŒãåŠçãããšãã³ãŒããçããèªã¿ããããªããŸãã
- setup ãš teardown
setup ããã³ teardown æ©èœãå©çšããŠãçµ±åãã¹ãã®å Žåãªã©ãè€éãªç°å¢ãæ§æã§ããŸãã
ãã¡ãžã³ã°ã䜿çšããŠããªãïŒcommunity mistakeïŒ
èŠçŽ
ãã¡ãžã³ã°ã¯ãè€éãªé¢æ°ãã¡ãœãããžã®ã©ã³ãã ãªãäºæ³å€ã®ããŸãã¯äžæ£ãªå ¥åãæ€åºããè匱æ§ããã°ãããã«ã¯æœåšçãªã¯ã©ãã·ã¥ãçºèŠããã®ã«å¹ççã§ãã
@jeromedoucet ããã®ãååã«æè¬ããããŸãã
æé©å
CPU ãã£ãã·ã¥ãç解ããŠããªã (#91)
- CPU ã¢ãŒããã¯ãã£
L1 ãã£ãã·ã¥ã¯ã¡ã€ã³ã¡ã¢ãªãããçŽ 50 ïœ 100 åé«éã§ãããããCPU ããŠã³ãã®ã¢ããªã±ãŒã·ã§ã³ãæé©åããã«ã¯ãCPU ãã£ãã·ã¥ã®äœ¿çšæ¹æ³ãç解ããããšãéèŠã§ãã
- ãã£ãã·ã¥ã©ã€ã³
ãã£ãã·ã¥ã©ã€ã³ã®æŠå¿µãæèããããšã¯ãããŒã¿éçŽåã¢ããªã±ãŒã·ã§ã³ã§ããŒã¿ãæŽçããæ¹æ³ãç解ããã®ã«éèŠã§ããCPU ã¯ã¡ã¢ãªãã¯ãŒãããšã«ãã§ããããŸããã代ããã«ãéåžžã¯ã¡ã¢ãªãããã¯ã 64 ãã€ãã®ãã£ãã·ã¥ã©ã€ã³ã«ã³ããŒããŸããåã ã®ãã£ãã·ã¥ã©ã€ã³ãæ倧éã«æŽ»çšããã«ã¯ã空éçå±ææ§ã匷å¶ããŠãã ããã
- æ§é äœã®ã¹ã©ã€ã¹ãšã¹ã©ã€ã¹ã®æ§é äœ
- äºæž¬å¯èœæ§
CPU ã«ãšã£ãŠäºæž¬å¯èœãªã³ãŒãã«ããããšã¯ãç¹å®ã®é¢æ°ãæé©åããå¹ççãªæ¹æ³ã§ããããŸããããšãã°ããŠããããŸãã¯å®æ°ã¹ãã©ã€ã㯠CPU ã«ãšã£ãŠäºæž¬å¯èœã§ãããéãŠãããã¹ãã©ã€ãïŒé£çµãªã¹ããªã©ïŒã¯äºæž¬ã§ããŸããã
- ãã£ãã·ã¥é 眮ããªã·ãŒ
ãã£ãã·ã¥ãããŒãã£ã·ã§ã³åãããŠããããšãèªèããããšã§ãé倧ãªã¹ãã©ã€ããåé¿ãããã£ãã·ã¥ã®ããäžéšã®ã¿ã䜿çšããããã«ããããšãã§ããŸãã
誀ã£ãå ±æãåŒãèµ·ãã䞊è¡åŠç(#92)
èŠçŽ
äžäœã¬ãã«ã® CPU ãã£ãã·ã¥ããã¹ãŠã®ã³ã¢ã§å ±æãããããã§ã¯ãªãããšãç¥ã£ãŠãããšã䞊è¡åŠçã«ãããã®èª€ã£ãå ±æãªã©ã§ããã©ãŒãã³ã¹ãäœäžãããŠããŸãããšãåé¿ã§ããŸããã¡ã¢ãªã®å ±æã¯ããããªãã®ã§ãã
åœä»€ã¬ãã«ã®äžŠåæ§ãèæ ®ããªã (#93)
èŠçŽ
åœä»€ã¬ãã«ã®äžŠåæ§ïŒILPïŒã䜿çšããŠã³ãŒãã®ç¹å®ã®éšåãæé©åããCPU ãã§ããã ãå€ãã®åœä»€ã䞊åå®è¡ã§ããããã«ããŸããããäž»ãªæé ã® 1 ã€ã«ããŒã¿ãã¶ãŒãã®ç¹å®ããããŸãã
ããŒã¿ã®é 眮ãæèããŠããªã (#94)
èŠçŽ
Goèšèªã§ã¯ãåºæ¬åã¯åã ã®ãµã€ãºã«åãããŠé 眮ãããããšãèŠããŠããããšã§ããããã¡ãªééããé¿ããããšãã§ããŸããããšãã°ãæ§é äœã®ãã£ãŒã«ãããµã€ãºã§éé ã«åç·šæãããšãæ§é äœãããã³ã³ãã¯ãã«ãªãïŒã¡ã¢ãªå²ãåœãŠãå°ãªããªãã空éçå±ææ§ãåäžããïŒå¯èœæ§ãããããšã«çæããŠãã ããã
ããŒããšã¹ã¿ãã¯ã®éããç解ããŠããªã (#95)
èŠçŽ
ããŒããšã¹ã¿ãã¯ã®åºæ¬çãªéããç解ããããšããGo ã¢ããªã±ãŒã·ã§ã³ãæé©åããéã«ã¯å€§åã§ããã¹ã¿ãã¯å²ãåœãŠã¯å®¹æãªã®ã«å¯ŸããŠãããŒãå²ãåœãŠã¯é ããã¡ã¢ãªã®ã¯ãªãŒã³ã¢ããã« GC ãå©çšããŸãã
å²ãåœãŠãæžããæ¹æ³ãããã£ãŠããªãïŒ API ã®å€æŽãã³ã³ãã€ã©ã®æé©åãããã³ sync.Pool
ïŒ (#96)
èŠçŽ
å²ãåœãŠãæžããããšããGo ã¢ããªã±ãŒã·ã§ã³ãæé©åããäžã§éèŠã§ããããã¯ãå
±æãé²ãããã« API ãæ
éã«èšèšãããäžè¬ç㪠Go ã³ã³ãã€ã©ã®æé©åãç解ãããsync.Pool
ã䜿çšãããªã©ãããŸããŸãªæ¹æ³ã§è¡ãããšãã§ããŸãã
ã€ã³ã©ã€ã³å±éãããŠããªã (#97)
èŠçŽ
ãã¡ã¹ããã¹ã®ã€ã³ã©ã€ã³åææ³ã䜿çšããŠãé¢æ°ã®åŒã³åºãã«ãããååŽæéãå¹ççã«åæžããŸãããã
Goèšèªã®èšºæããŒã«ãå©çšããŠããªã (#98)
èŠçŽ
ãããã¡ã€ãªã³ã°ãšå®è¡ãã¬ãŒãµãå©çšããŠãã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹ãšæé©åãã¹ãéšåã«ã€ããŠç解ããŸãããã
ã»ã¯ã·ã§ã³å šæã¯ãã¡ãã
GC ã®ä»çµã¿ãç解ããŠããªã (#99)
èŠçŽ
GC ã®èª¿æŽæ¹æ³ãç解ãããšãçªç¶ã®è² è·ã®å¢å ãããå¹ççã«åŠçã§ãããªã©ãããŸããŸãªæ©æµãåŸãããŸãã
Docker ãš Kubernetes äžã§Goèšèªãå®è¡ããããšã®åœ±é¿ãç解ããŠããªã (#100)
èŠçŽ
Docker ãš Kubernetes ã«ãããã€ããéã® CPU ã¹ããããªã³ã°ãåé¿ããã«ã¯ãGoèšèªã CFS 察å¿ã§ã¯ãªãããšã«çæããŠãã ããã