1. ã¯ããã«ïŒGoèšèªã®ç»å Ž
è¿å¹Žãããã°ã©ãã³ã°èšèªã®äžã§æ³šç®ãéããŠãããGoèšèªãããã®ååãè³ã«ããããšãããæ¹ãå€ãã§ããããããããGoèšèªãã©ã®ãããªèæ¯ã§çãŸããã©ã®ãããªç¹åŸŽãæã£ãŠããã®ããå ·äœçã«ç¥ã£ãŠããæ¹ã¯å°ãªããããããŸããããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªã®èæ¯ãšç¹åŸŽã«ã€ããŠããããããã解説ããŸãã
1.1 Goèšèªã®èæ¯
Goèšèªã¯ã2007幎ã«Googleã®å éšãããžã§ã¯ããšããŠéå§ãããŸãããåœæãGoogleã®ãšã³ãžãã¢ãã¡ã¯ã倧èŠæš¡ãªã·ã¹ãã ãå¹ççã«éçºã»éçšããããã®æ°ããèšèªãæ±ããŠããŸãããåŸæ¥ã®èšèªã§ã¯ãã¹ã±ãŒã©ããªãã£ã䞊è¡åŠçã®é¢ã§ã®èª²é¡ãå€ãååšããŠããŸãããããã§ãRobert GriesemerãRob PikeãKen Thompsonã®3人ã®ãšã³ãžãã¢ãäžå¿ãšãªãããããã®èª²é¡ã解決ããããã®æ°ããèšèªãGoãã®éçºãå§ãŸããŸããã
Goèšèªã¯ãã·ã³ãã«ããè¿œæ±ãã€ã€ãé«æ§èœãã€å¹ççãªéçºãå¯èœãªèšèªãšããŠèšèšãããŸããããããŠã2009幎ã«å ¬åŒã«ãªãªãŒã¹ãããå€ãã®éçºè ããé«ãè©äŸ¡ãåããããšãšãªããŸããã
1.2 Goèšèªã®ç¹åŸŽ
Goèšèªã®æ倧ã®ç¹åŸŽã¯ã䞊è¡åŠçãç°¡åã«å®çŸã§ããããšã§ããGoèšèªã«ã¯ãgoroutineããšãã軜éãªã¹ã¬ããã®ãããªãã®ãååšãããããçšããããšã§ãå€æ°ã®ã¿ã¹ã¯ãåæã«å®è¡ããããšãã§ããŸããããã«ãããé«è² è·ãªç¶æ³ã§ãã¹ã ãŒãºã«åäœããã¢ããªã±ãŒã·ã§ã³ãéçºããããšãå¯èœãšãªããŸããã
ãŸããGoèšèªã¯éçåä»ãèšèªã§ãããªãããåçèšèªã®ãããªæè»æ§ãæã£ãŠããŸããããã«ãããéçºè ã¯å®å šæ§ãä¿ã¡ã€ã€ãè¿ éãªéçºãé²ããããšãã§ããŸããããã«ãGoèšèªã®æšæºã©ã€ãã©ãªã¯éåžžã«å å®ããŠãããå€ãã®æ©èœãç°¡åã«å©çšããããšãã§ããŸãã
ãã®ãããªç¹åŸŽãæã€Goèšèªã¯ãWebã¢ããªã±ãŒã·ã§ã³ã®éçºãããã·ã¹ãã ããŒã«ã®äœæãŸã§ãå¹ åºãçšéã§å©çšãããŠããŸãã
2. Goèšèªã®åºæ¬æŠå¿µ
ããã°ã©ãã³ã°èšèªãåŠã¶éããã®èšèªåºæã®åºæ¬æŠå¿µãç解ããããšã¯éåžžã«éèŠã§ããGoèšèªãäŸå€ã§ã¯ãããŸããããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªã®åºæ¬çãªæŠå¿µã«ã€ããŠããããããã解説ããŸãã
2.1 å€æ°ãšããŒã¿å
ããã°ã©ãã³ã°ã«ãããå€æ°ã¯ãããŒã¿ãä¿åããããã®ã容åšãã®ãããªãã®ã§ããGoèšèªã§ã¯ãå€æ°ã宣èšããéã«ããã®å€æ°ã®ããŒã¿åãæå®ããå¿ èŠããããŸããäŸãã°ãæŽæ°ãä¿åããå€æ°ã宣èšããå ŽåãintãšããããŒã¿åã䜿çšããŸãã
Goèšèªã®äž»ãªããŒã¿åã«ã¯ä»¥äžã®ãããªãã®ããããŸãïŒ
ããŒã¿å | 説æ |
---|---|
int | æŽæ°ãè¡šãããŒã¿å |
float64 | æµ®åå°æ°ç¹æ°ãè¡šãããŒã¿å |
bool | çåœå€ïŒtrueãŸãã¯falseïŒãè¡šãããŒã¿å |
string | æååãè¡šãããŒã¿å |
2.2 å¶åŸ¡æ§æãšã«ãŒã
å¶åŸ¡æ§æã¯ãããã°ã©ã ã®æµããå¶åŸ¡ããããã®æ§æã§ããGoèšèªã«ã¯ãifãswitchãªã©ã®å¶åŸ¡æ§æãååšããŸãããããã®æ§æã䜿çšããããšã§ãç¹å®ã®æ¡ä»¶äžã§ã®ã¿ã³ãŒããå®è¡ããããšãã§ããŸãã
äžæ¹ãã«ãŒãã¯ãç¹å®ã®ã³ãŒããç¹°ãè¿ãå®è¡ããããã®æ§æã§ããGoèšèªã§ã¯ãforã«ãŒããæäŸãããŠãããããã䜿çšããŠç¹°ãè¿ãåŠçãè¡ãããšãã§ããŸãã
2.3 é¢æ°ãšããã±ãŒãž
é¢æ°ã¯ãç¹å®ã®ã¿ã¹ã¯ãå®è¡ããããã®ã³ãŒãã®ãŸãšãŸãã§ããGoèšèªã§ã¯ãé¢æ°ãå®çŸ©ããããšã§ãåãã³ãŒããäœåºŠãæžãããšãªããå¿ èŠãªå Žæã§ãã®é¢æ°ãåŒã³åºãããšãã§ããŸãã
äžæ¹ãããã±ãŒãžã¯ãé¢æ°ãå€æ°ãªã©ã®ã³ãŒãã®ãŸãšãŸãã管çããããã®ä»çµã¿ã§ããGoèšèªã®æšæºã©ã€ãã©ãªã«ã¯ãå€ãã®äŸ¿å©ãªããã±ãŒãžãæäŸãããŠããããããå©çšããããšã§ãå¹ççã«ããã°ã©ã ãéçºããããšãã§ããŸãã
3. Goèšèªã§ã§ããããš
Goèšèªã¯ãã®èšèšææ³ãç¹åŸŽãããå€å²ã«ãããéçºé åã§ã®å©çšãå¯èœã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªã䜿çšããŠå®çŸã§ããäž»ãªã¿ã¹ã¯ãã¢ããªã±ãŒã·ã§ã³ã®éçºã«ã€ããŠãå ·äœçã«è§£èª¬ããŸãã
3.1 Webã¢ããªã±ãŒã·ã§ã³ã®éçº
Goèšèªã¯ãé«éãã€ã¹ã±ãŒã©ãã«ãªWebã¢ããªã±ãŒã·ã§ã³ã®éçºã«é©ããŠããŸããGoã®æšæºã©ã€ãã©ãªã«ã¯ãHTTPãµãŒããŒãã¯ã©ã€ã¢ã³ãã®æ©èœãçµã¿èŸŒãŸããŠããããããå©çšããããšã§ãç°¡åã«Webã¢ããªã±ãŒã·ã§ã³ãAPIãéçºããããšãã§ããŸãããŸããGoèšèªã®äžŠè¡åŠçã®ç¹åŸŽã掻ããããšã§ãå€æ°ã®ãŠãŒã¶ãŒããã®ãªã¯ãšã¹ããå¹ççã«åŠçããããšãå¯èœã§ãã
3.2 ã·ã¹ãã ããŒã«ã®äœæ
Goèšèªã¯ãã·ã¹ãã ããããã¯ãŒã¯ã®ããŒã«ãäœæããã®ã«ãé©ããŠããŸããç¹ã«ãã¯ãã¹ãã©ãããã©ãŒã ã®ããŒã«ãäœæããéã«ã¯ãGoèšèªã®ç¹æ§ã倧ãã«åœ¹ç«ã¡ãŸããGoã§æžãããã³ãŒãã¯ãããŸããŸãªOSãã¢ãŒããã¯ãã£ã§åäœãããã€ããªãçæããããšãã§ãããããäžåºŠã®éçºã§å€ãã®ç°å¢ã«å¯Ÿå¿ããããŒã«ãäœæããããšãã§ããŸãã
3.3 ããŒã¿ããŒã¹æäœ
ããŒã¿ããŒã¹ãšã®é£æºã¯ãå€ãã®ã¢ããªã±ãŒã·ã§ã³éçºã«ãããŠæ¬ ãããªãèŠçŽ ã§ããGoèšèªã§ã¯ãæ§ã ãªããŒã¿ããŒã¹ãšã®æ¥ç¶ãæäœããµããŒãããã©ã€ãã©ãªãæäŸãããŠããŸããããã«ãããSQLããŒã¹ã®ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ãããNoSQLããŒã¿ããŒã¹ãŸã§ãå¹ åºãããŒã¿ããŒã¹ãå¹ççã«æäœããããšãã§ããŸãã
3.4 ãã€ã¯ããµãŒãã¹ã®å®è£
ãã€ã¯ããµãŒãã¹ã¯ã倧èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³ãå°ããªãµãŒãã¹ã«åå²ããŠéçºã»éçšããã¢ãŒããã¯ãã£ã®ããšãæããŸããGoèšèªã¯ããã®è»œéæ§ãé«æ§èœãªç¹æ§ããããã€ã¯ããµãŒãã¹ã®å®è£ ã«éåžžã«é©ããŠããŸããç¹ã«ããããã¯ãŒã¯éä¿¡ã®é«éåãã䞊è¡åŠçã®å¹ççãªå®è£ ãæ±ããããå Žé¢ã§ãGoèšèªã®åãæ倧éã«çºæ®ããããšãã§ããŸãã
4. Goèšèªã®éçºç°å¢ã®æ§ç¯
GoèšèªãåŠã¶ããã®ç¬¬äžæ©ã¯ãé©åãªéçºç°å¢ãæ§ç¯ããããšã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªã®éçºç°å¢ãæŽããããã®æé ããéçºã«åœ¹ç«ã€ããŒã«ã«ã€ããŠè§£èª¬ããŸãã
4.1 Goã®ã€ã³ã¹ããŒã«æ¹æ³
Goèšèªã䜿çšããã«ã¯ããŸããã®å®è¡ç°å¢ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸããå ¬åŒã®Goèšèªã®ãŠã§ããµã€ããããææ°ã®ããŒãžã§ã³ã®ã€ã³ã¹ããŒã©ãããŠã³ããŒãããããšãã§ããŸããã€ã³ã¹ããŒã©ãå®è¡ããããšã§ãGoã®ã³ã³ãã€ã©ãæšæºã©ã€ãã©ãªãªã©ãåºæ¬çãªããŒã«ãã€ã³ã¹ããŒã«ãããŸãã
ã€ã³ã¹ããŒã«ãå®äºããããã³ãã³ãã©ã€ã³ãã¿ãŒããã«ãéãããgo versionããšããã³ãã³ããå®è¡ããããšã§ãæ£ããã€ã³ã¹ããŒã«ããããã確èªããããšãã§ããŸãã
4.2 éçºããŒã«ã®çŽ¹ä»
Goèšèªã®éçºãå¹ççã«è¡ãããã«ã¯ãããã€ãã®ããŒã«ã圹ç«ã¡ãŸããäŸãã°ãGoLandãVisual Studio Codeãªã©ã®çµ±åéçºç°å¢(IDE)ã¯ãã³ãŒãã®è£å®ããããã°ãªã©ãå€ãã®äŸ¿å©ãªæ©èœãæäŸããŠããŸãã
ãŸããGoèšèªã«ã¯ãgo fmtãããgo vetããšãã£ãã³ãã³ãã©ã€ã³ããŒã«ãæäŸãããŠããããããã䜿çšããããšã§ãã³ãŒãã®ãã©ãŒããããæŽããããæœåšçãªåé¡ãæ€åºããããšãã§ããŸãã
4.3 å®éã®ã³ãŒãã£ã³ã°ã®æµã
Goèšèªã§ã®ããã°ã©ãã³ã°ã¯ãéåžžã«ã·ã³ãã«ã§çŽæçã§ãããŸããæ°ãããããžã§ã¯ããéå§ããã«ã¯ããgo mod init ãããžã§ã¯ãåããšããã³ãã³ãã䜿çšããŠãæ°ããã¢ãžã¥ãŒã«ãäœæããŸãã次ã«ã.goãšããæ¡åŒµåã®ãã¡ã€ã«ãäœæãããã®äžã«ã³ãŒããèšè¿°ããŠãããŸãã
ã³ãŒãã®èšè¿°ãå®äºãããããgo run ãã¡ã€ã«å.goããšããã³ãã³ããå®è¡ããããšã§ãããã°ã©ã ãå®è¡ããããšãã§ããŸãããŸããããã°ã©ã ã«åé¡ããã£ãå ŽåãGoèšèªã¯ãããããããšã©ãŒã¡ãã»ãŒãžã衚瀺ããŠããããããåé¡ã®åå ãè¿ éã«ç¹å®ããä¿®æ£ããããšãã§ããŸãã
ãã®ããã«ãGoèšèªã®éçºç°å¢ã¯ãåå¿è ã§ãç°¡åã«åãçµãããšãã§ããããã«èšèšãããŠããŸããé©åãªããŒã«ã䜿çšããããšã§ãããå¹ççãã€æ¥œããããã°ã©ãã³ã°ãé²ããããšãã§ããã§ãããã
5. Goèšèªã®å®çšäŸ
Goèšèªã¯ããã®æ§èœãç¹æ§ããå€ãã®å Žé¢ã§æŽ»çšãããŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªãå®éã«ã©ã®ãããªå Žé¢ã§äœ¿çšãããŠããã®ããå ·äœçãªäºäŸãããšã«è§£èª¬ããŸãã
5.1 äŒæ¥ã§ã®å©çšäºäŸ
å€ãã®å€§æäŒæ¥ããGoèšèªã®é«éæ§ãå®å®æ§ã掻ãããŠãæ§ã ãªãµãŒãã¹ãã¢ããªã±ãŒã·ã§ã³ã®éçºã«åãçµãã§ããŸããäŸãã°ãGoogleã¯GoèšèªãéçºããäŒæ¥ãšããŠç¥ãããå éšã®å€ãã®ã·ã¹ãã ããµãŒãã¹ã§Goã䜿çšãããŠããŸãããŸããDockerãKubernetesãšãã£ããã¯ã©ãŠãé¢é£ã®æè¡ãGoèšèªã§éçºãããŠããããã®æ§èœãæ¡åŒµæ§ãé«ãè©äŸ¡ãããŠããŸãã
ããã«ãå€ãã®ã¹ã¿ãŒãã¢ããäŒæ¥ããGoèšèªã®éçºé床ã®éãããã¹ã±ãŒã©ããªãã£ã®é«ãã掻ãããŠãæ°ãããµãŒãã¹ã®éçºã«åãçµãã§ããŸããããã«ãããçæéã§ã®ãããã¿ã€ãã³ã°ãã倧éã®ãŠãŒã¶ãŒãžã®å¯Ÿå¿ãå¯èœãšãªã£ãŠããŸãã
5.2 å人ãããžã§ã¯ãã§ã®æŽ»çšæ¹æ³
Goèšèªã¯ãå人ã®ãããžã§ã¯ãã趣å³ã®éçºã«ãéåžžã«é©ããŠããŸãããã®çç±ãšããŠãGoèšèªã¯åŠç¿ã³ã¹ããäœãããšãæšæºã©ã€ãã©ãªãå å®ããŠããããšãªã©ãæããããŸããããã«ãããåå¿è ã§ãçæéã§å®çšçãªã¢ããªã±ãŒã·ã§ã³ãããŒã«ãéçºããããšãã§ããŸãã
äŸãã°ãWebã¹ã¯ã¬ã€ãã³ã°ãããŒã¿è§£æãç°¡åãªWebã¢ããªã±ãŒã·ã§ã³ãªã©ãå€å²ã«ããããããžã§ã¯ãã§Goèšèªã掻çšãããŠããŸãããŸããGoèšèªã®ã³ãã¥ããã£ãéåžžã«æŽ»çºã§ãå€ãã®ã©ã€ãã©ãªããã¬ãŒã ã¯ãŒã¯ãæäŸãããŠããããããããã掻çšããããšã§ãããã«å¹ççãªéçºãå¯èœãšãªã£ãŠããŸãã
6. GoèšèªãåŠã¶ããã®ãªãœãŒã¹
GoèšèªãåŠã¶ããã®ãªãœãŒã¹ã¯ãå€å²ã«ããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãGoèšèªã®åŠç¿ã«åœ¹ç«ã€ãªã³ã©ã€ã³ææãæžç±ãã³ãã¥ããã£ãã€ãã³ãã«ã€ããŠçŽ¹ä»ããŸãã
6.1 ãªã³ã©ã€ã³ææãšæžç±
Goèšèªã®åŠç¿ã«ã¯ãå€ãã®ãªã³ã©ã€ã³ææãæžç±ãå©çšã§ããŸããç¹ã«ãå ¬åŒã®ããã¥ã¡ã³ãã¯ãGoèšèªã®åºæ¬çãªæŠå¿µãæ©èœã詳ãã解説ããŠãããåå¿è ããäžçŽè ãŸã§å¹ åºãå©çšãããŠããŸãããŸããGo Playgroundã¯ããã©ãŠã¶äžã§Goã®ã³ãŒããå®è¡ã§ãããµãŒãã¹ã§ãæ軜ã«Goèšèªã®æåãè©Šãããšãã§ããŸãã
æžç±ã«é¢ããŠã¯ãå€ãã®åªããGoèšèªã®å ¥éæžãå®è·µæžãåºçãããŠããŸãããããã®æžç±ãéããŠãããæ·±ãGoèšèªãåŠã¶ããšãã§ããã§ãããã
6.2 ã³ãã¥ããã£ãšã€ãã³ã
Goèšèªã®ã³ãã¥ããã£ã¯éåžžã«æŽ»çºã§ãäžçäžã§å€ãã®ã€ãã³ããããŒãã¢ãããéå¬ãããŠããŸãããããã®ã€ãã³ãã§ã¯ãGoèšèªã®ææ°ã®ååãæè¡çãªãããã¯ã«ã€ããŠåŠã¶ããšãã§ããã ãã§ãªããä»ã®Goèšèªã®ãŠãŒã¶ãŒãšäº€æµããæ©äŒãåŸãããŸãã
ç¹ã«ãGoConãGopherConãšãã£ã倧èŠæš¡ãªã«ã³ãã¡ã¬ã³ã¹ã¯ãGoèšèªã®ã³ãã¥ããã£ã«ãããäž»èŠãªã€ãã³ããšããŠç¥ãããŠããŸãããããã®ã€ãã³ãã«åå ããããšã§ãæå 端ã®Goèšèªã®æè¡ãç¥èãç¿åŸããããšãã§ããã§ãããã
ãŸããå€ãã®éœåžã«ã¯Goèšèªã®ãŠãŒã¶ãŒã°ã«ãŒããååšããå®æçã«ããŒãã¢ãããå匷äŒãéå¬ãããŠããŸãããããã®ã€ãã³ãã¯ãå°åã®Goèšèªã®ãŠãŒã¶ãŒãšã®äº€æµããå®éã®éçºã®çŸå Žã§ã®çµéšãå ±æããå ŽãšããŠéåžžã«äŸ¡å€ããããŸãã
7. ãŸãšãïŒGoèšèªã®æªæ¥ãšå¯èœæ§
ãã®èšäºãéããŠãGoèšèªã®åºæ¬çãªç¹åŸŽãå©çšäºäŸãåŠã³æ¹ãªã©ã«ã€ããŠè©³ãã解説ããŠããŸãããæåŸã«ãGoèšèªã®æªæ¥ããã®æã€å¯èœæ§ã«ã€ããŠèå¯ããŸãã
Goèšèªã¯ããã®ç»å Žããçãæéã§å€ãã®éçºè ãã泚ç®ãéããå€ãã®ãããžã§ã¯ããäŒæ¥ã§ã®æ¡çšãé²ããããŠããŸãããã®èæ¯ã«ã¯ãGoèšèªã®æã€ã·ã³ãã«ããé«ãããã©ãŒãã³ã¹ããããŠåŒ·åãªæšæºã©ã€ãã©ãªãªã©ã®ç¹åŸŽãæããããŸãã
ãŸããã¯ã©ãŠãæè¡ã®æ®åãšãšãã«ããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã®æ¡çšãå¢ããŠããçŸä»£ã«ãããŠãGoèšèªã¯ãã®ç¹æ§ãæ倧éã«æŽ»ããããšãã§ããèšèªãšããŠäœçœ®ã¥ããããŠããŸããç¹ã«ãDockerãKubernetesãšãã£ãæè¡ã®èåŸã«Goèšèªãæ¡çšãããŠããããšã¯ããã®å®çžŸãç©èªã£ãŠããŸãã
Goèšèªã®ã³ãã¥ããã£ãéåžžã«æŽ»çºã§ãæ°ããã©ã€ãã©ãªãããŒã«ããã¬ãŒã ã¯ãŒã¯ãæ¥ã éçºãããŠããŸãããã®ãããªèæ¯ãããGoèšèªã®æªæ¥ã¯éåžžã«æããããããªãçºå±ãæåŸ ãããŠããŸãã
æåŸã«ãæè¡ã®éžæã¯ãããžã§ã¯ãã®èŠä»¶ãç®çã«å¿ããŠè¡ãã¹ãã§ãããGoèšèªã¯ãã®å€æ§ãªå©çšã·ãŒã³ãšé«ãæ¡åŒµæ§ãããå€ãã®éçºè ã«ãšã£ãŠé åçãªéžæè¢ãšãªãã§ããããä»åŸãGoèšèªã®ååã«æ³šç®ããŠããã®å¯èœæ§ãè¿œæ±ããŠãããããšæããŸãã