Goãã¡ãã£ã¨ä½¿ã£ã¦ã¿ã
ãããªã大人æ°ã®goãããã触ããRob PikeãKen Thompsonã¨ããä½ã£ã¦ããã¨ããã ãã§ãããã¨æã£ã¦ãã¾ãã¾ãã
ãã«ã
ubuntuã®å ´åã http://golang.org/doc/install.html ã«æ¸ãã¦ããã¨ããããã°ã³ã³ãã¤ã©ããªã³ã«ãªã©ã®ãã¼ã«ã®ãã¤ããªãã§ããããã¾ãã
ã³ã³ãã¤ã©ããªã³ã«ã¯ã¢ã¼ããã¯ãã£ãã¨ã«ååãéãã
- 386ç¨: 8g, 8l
- amd64ç¨: 6g, 6l
- armç¨: 5g, 5l
ã®ããã§ã(FAQã«ããã¨ãPlan9ã®ä¼çµ±ããã)ã
ã¾ããç°å¢å¤æ°GOROOT,GOOS,GOARCHã¯ãã³ã³ãã¤ã©ããªã³ã«ã®å®è¡æã«ãå¿ è¦ãªãã®ã§ã(çæãã¤ããªã®å®è¡ã«ã¯ä¸è¦)ã.bashrcã«ããã¨ãã¨ããsourceã³ãã³ããªã©ã§ç°¡åã«åãè¾¼ããããã«ãã¨ãã¨ããã§ãããã
emacsã¢ã¼ã
ã½ã¼ã¹ä¸ã®miscã«emacsãvimãMacã®Xcodeã®ããã®goã¢ã¼ããå ¥ã£ã¦ã¾ãã
emacsã®å ´åãgo-mode-load.elã¨go-mode.elãããã¾ãããã®ãµãã¤ã®ãã¡ã¤ã«ã~/.emacs.d/ã®ç´ä¸ã«ã³ãã¼ãã~/.emacs.elã«
; ~/.emacs.d/ã«éè¯ã¢ã¼ããªã©ãç½®ããããã«ããè¨å®ã (setq load-path (cons (expand-file-name "~/.emacs.d/") load-path)) (require 'go-mode-load) ; å®è³ªgo-modeã®ããã«æ¸ãã®ã¯ããã ã
ã¨ããã°æ¡å¼µåã".go"ãªãã¡ã¤ã«ãéãã°ãgoã¢ã¼ãã«ãªãã¾ãã
ä¾ããã°ã©ã : exp/evalã§æå®è¡
æåã®ã»ãã«æ¸ãããã°ã©ã ã¨ããã®ã¯ããã®äººã®ç¹å¾´ãã§ã¦ãããããã¾ãããèªåã®å ´åãevalããããã©ããã調ã¹ã¦ãã¾ã(ã¤ã¾ãæåã¯ã¢ããªã«ããªã)ã
// 8g doeval.go // 8l -o doeval doeval.8 package main import "exp/eval" func main() { world := eval.NewWorld(); code, err := world.Compile(` print("Hello\n"); `); // does not supoort package if err != nil { panic(err.String(), "\n"); } else { if value, err := code.Run(); err != nil { panicln(err.String()); } else println(value); } }
以ä¸ã注ç®ããç¹å¾´
ã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«
ã¾ããgoã®ã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ã§ã®ã¤ã³ãã³ãã¯ãã¼ãã¿ãã®ããã§ããã½ã¼ã¹ä»å±ã®ãã¹ãã³ã¼ããããã ãããã®è¨å®ã¯ãã£ã¡ããã¼ã«gofmtã®ããã©ã«ããemacsã¢ã¼ããã¡ã¤ã«ã§ããªããã¦ãã¾ãã
exp/evalããã±ã¼ã¸
exp/evalã«ã¯ç°¡åãªæã¤ã³ã¿ããªã¿ãå®è£ ããã¦ãã¾ãããã®æ©è½ã§ã¯interfaceãpackage/importã®è§£éã¯å®è£ ããã¦ãã¾ããã
ããã±ã¼ã¸å
ããã±ã¼ã¸åã¯åä¸èå¥åã®ã¿ã§ã使ãå´ãimportæã«ååãå¤ãã¦å²ãå½ã¦ããã¨ã§ã使ãåããä»çµã¿ã®ããã§ãããã¨ãã°ä»¥ä¸ã®ãããªãããã§ãããã:
import fooDom "foo/dom" import barDom "bar/dom"
ã¾ããmain.mainã¨ããé¢æ°ããã³ãã³ãå®è¡ã§èµ·åãããé¢æ°ã«ãªã£ã¦ãã¾ãã
å¤æ°å®£è¨ã®ç°¡ç¥å½¢å¼
å¤æ°å®£è¨ã¯è¤æ°ã®å½¢å¼ãããã¾ãã
Cã®ããã«ååã®å¤æ°ããã¶ãã¦å®£è¨ã§ããããã§ãã
è¤åæã®ã»ãã³ãã³çç¥
æãåºåãã»ãã³ãã³ã¯ããã¤ãã®å ´é¢ã§çç¥å¯è½ã§ãã
rubyãªã©ã¨ã¡ããã"}"ã¨å®£è¨ã§ã®")"ã®åã ãã¨ã¨ã¦ãéããã¦ãã¾ãã
ã¤ã¾ããä¾ã³ã¼ãã ã¨panicãpanicln, printlnã®ãã¨ã®ã»ãã³ãã³ã¯çç¥å¯è½ã§ãã
æååãªãã©ã«
ããã¯ã¯ãªã¼ãã§ãããæååãªãã©ã«ã¯ãæ¹è¡ããã¼ãã¿ããå«ããã®ã¾ã¾ã®å 容ãæååã«ãªãã¾ãã
ifæ
ifæã¯ãªãã·ã§ã³ã§æ¡ä»¶ã®åã«ä»£å ¥æãªã©ã®åç´æãä¸ã¤åãè¾¼ãã¾ãã
ããä¸ã¤å¤ãã£ã¦ãã®ã¯ãifæã®çé¨ã¯blockæã«ããªãã¦ã¯ãããªãããå½é¨ã¯ä»»æã®æã使ããã¨ããã¨ãããã§ããããã
æ¡ä»¶å¼
æ¡ä»¶å¼ã¯"bool"å¤("true"/"false")ã®ã¿ã¤ããã¾ãããã®ãã(ãã¤ã³ã¿åãã¹ã©ã¤ã¹åã®)å¤æ°ã«å¤ããããã©ããã®å¤å®ã§ã¯ã"nil"ã¨æ¯è¼ãããã¨ã«ãªãã¾ãã
çµã¿è¾¼ã¿é¢æ°
"print"ã"panic"ã¯çµã¿è¾¼ã¿ã®é¢æ°ã§ãã
ãã£ã¨æ®ããã©ããã¯ä¿è¨¼ããªãããã§ããã
ãã®ä»ä»æ§ã§æ³¨ç®ããç¹
"++"/"--"ãä»£å ¥ãå¼ã§ã¯ãªãæã«ãªã£ã¦ãã
ãã®ãããifãswitchã§ãªãã·ã§ã³ã§æãä¸ã¤æ¸ããããã«ããã¨ããæãã§ããããã
ãªãã¬ã¯ã·ã§ã³ãªã©ãããunsafeããã±ã¼ã¸
interfaceå
åã·ã¹ãã ä¸ã¯ãã¡ã½ããã»ããã®å å«é¢ä¿ã§ãã§ãã¯ããã
- http://golang.org/doc/go_spec.html#Interface_types
- http://golang.org/doc/go_spec.html#Type_identity_and_compatibility
èå³æ·±ãã®ã¯structã«åãè¾¼ããã¨ãã§ããç¹ã§ããã®ã¾ã¾åinterfaceããã¤ãã¨ã«ãªãããã (structã©ããã§ãinterfaceã©ããã§ãã§ãããã©)ã
ãã¨ç´°ããç¹ã ã¨ãã¬ã·ã¼ãã¯ãã¤ã³ã¿ã§ãããinterfaceã®å ´åãstructãã¤ã³ã¿ã¨éãnilå¤ã«å¯¾ãã¦ãã¡ã½ãããå¼ã¹ããªãã§ããã
ããã±ã¼ã¸ã®inité¢æ°
ãããã£ãæ©è½ã¯æè¿ã®è¨èªã§ã¯æ®éã ãã©ã
è¨è¨å¤æã®å¤ãã¯FAQã«ãããã®ã§å¿ è¦ã§ãã