Mac(homebrew)ã§Goã®ã¤ã³ã¹ãã¼ã«ã¨ãEmacsã®ç°å¢æ§ç¯
Goã®åå¼·ã®ããã¾ãHello worldã¨Emacsã®ç°å¢ãæ§ç¯ãããã®ã§ããã®è¨é²ã
ãã®è¨äºã®ã´ã¼ã«
- homebrewã§Goã®ã¤ã³ã¹ãã¼ã«
- godocã«ããããã¥ã¡ã³ãã®è¡¨ç¤º
- Hello worldã表示ãã
- Emacsã®ç°å¢æ§ç¯
- go-autocompleteã§ä¿ç®¡
- M-x godocã§ããã¥ã¡ã³ã表示
- goflaymakeã§æ§æãã§ãã¯ãªã©
- godefã§å®ç¾©ã¸ã£ã³ã
goãhomebrewã§ã¤ã³ã¹ãã¼ã«ãã
$ brew install go
ã¤ã³ã¹ãã¼ã«ããã¦ããã確èª
$ go version go version go1.2 darwin/amd64
Hello worldãæ¸ãã¦ã¿ã
é©å½ãªå ´æã§ä»¥ä¸ã®ãã¡ã¤ã«ï¼hello.goï¼ãä½æ
package main import "fmt" func main() { fmt.Println("Hello World!") }
ãã«ããã¦å®è¡
$ go build hello.go $ ./hello Hello World!
godocãã¤ã³ã¹ãã¼ã«ãã¦ããã¥ã¡ã³ãã表示ãã
godocã使ãã¨ã³ãã³ãã®ããã¥ã¡ã³ãã表示ã§ããããã«ãªãã以ä¸ã®ããã«ä½¿ãã
$ godoc fmt Println PACKAGE DOCUMENTATION package fmt import "fmt" FUNCTIONS func Println(a ...interface{}) (n int, err error) Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
godocãã¤ã³ã¹ãã¼ã«ããã«ã¯go getã使ããããªãã ãã©ãç°å¢å¤æ°$GOPATHãè¨å®ãã¦ããªãã¨ã ããããã.zshrcãªã©ã«ä»¥ä¸ã®ããã«è¨å®ããã
# for go lang if [ -x "`which go`" ]; then export GOROOT=`go env GOROOT` export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin fi
GOROOT/binãPATHç°å¢å¤æ°ã«è¿½å ãã¦ããã®ã¯ãgodocã®ã³ãã³ãã®ã¤ã³ã¹ãã¼ã«å ãããã«ãªããããï¼é常ã¯ãGOPATH/binã«ã¤ã³ã¹ãã¼ã«ããããããï¼
Go 1.2 (devel) doesn't install godoc into bin · Issue #23281 · Homebrew/legacy-homebrew · GitHub
以ä¸ã®ã³ãã³ãã§godocãã¤ã³ã¹ãã¼ã«ã
$ go get code.google.com/p/go.tools/cmd/godoc
ã¡ãªã¿ã«ã$GOPATHãè¨å®ãã¦ãªãã¨ä»¥ä¸ã®æ§ãªã¨ã©ã¼ã¨ãªãã
$ go get code.google.com/p/go.tools/cmd/godoc package code.google.com/p/go.tools/cmd/godoc: cannot download, $GOPATH not set. For more details see: go help gopath
Emacsã®ç°å¢è¨å®
次ã®è¨äºãåèã«å¿ è¦ãªãã®ãã¤ã³ã¹ãã¼ã«ããã
goå´ã®å¿ è¦ãªããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã
$ go get code.google.com/p/go.tools/cmd/godoc $ go get code.google.com/p/rog-go/exp/cmd/godef $ go get github.com/nsf/gocode $ go get -u github.com/dougm/goflymake
list-packagesã§ä»¥ä¸ãã¤ã³ã¹ãã¼ã«ãã¦ãã
- go-mode
- go-autocomplete
- auto-complete-modeï¼ã¤ã³ã¹ãã¼ã«æ¸ã¿ã ã£ãã®ã§ãããã§ã¯ç¹ã«èª¬æããªãï¼
- flyamke(ã¤ã³ã¹ãã¼ã«æ¸ã¿ã ã£ãã®ã§ãããã§ã¯ç¹ã«èª¬æããªãï¼
init.elã«ä»¥ä¸ã追å ã
;; PATHã®è¨å®ãã·ã§ã«ã®è¨å®ããæã£ã¦ãã ;; $SHELLã§è¨å®ããã¦ããã·ã§ã«ã®è¨å®ã使ããã (when (memq window-system '(mac ns)) (exec-path-from-shell-initialize)) ;; ============================================== ;; goã®è¨å® ;; ============================================== (when (and (require 'go-mode nil t) (executable-find "go")) ;; GOROOT, GOPATHç°å¢å¤æ°ãèªã¿è¾¼ã (let ((envs '("GOROOT" "GOPATH"))) (exec-path-from-shell-copy-envs envs)) ;; go-autocompleteã®èªã¿è¾¼ã¿ (eval-after-load "go-mode" '(progn (require 'go-autocomplete))) ;; goflaymakeã®èªã¿è¾¼ã¿ (add-to-list 'load-path (concat (getenv "GOPATH") "/src/github.com/dougm/goflymake")) (require 'go-flymake) ;; go-eldocã®è¨å® ;; https://github.com/syohex/emacs-go-eldoc (add-hook 'go-mode-hook 'go-eldoc-setup) (set-face-attribute 'eldoc-highlight-function-argument nil :underline t :foreground "green" :weight 'bold) )
ä»åãexec-path-from-shellã使ã£ã¦.zshrcã§è¨å®ããPATHãç°å¢å¤æ°(GOROOT, GOPATH)ãèªã¿è¾¼ãããã«ãã¦ããã
- EmacsでPATHの設定が引き継がれない問題をエレガントに解決する - Qiita
- Emacsでの環境変数の設定に exec-path-from-shellを使うようにした - syohex’s diary
ã¡ãªã¿ã«ãexec-path-from-shellã¯$SHELLç°å¢å¤æ°ã«è¨å®ããã¦ããã·ã§ã«ã®ç°å¢å¤æ°ãã²ã£ã±ã£ã¦ããããããããã©ã«ãã®ã·ã§ã«ãå¤æ´ããã«ã¯ä»¥ä¸ã®ããã«ããï¼ããã§ã¯ãbashããzshã«å¤æ´ï¼
$ echo $SHELL /bin/bash $ chsh -s /bin/zsh # ããã§åèµ·å # åèµ·åå¾ä»¥ä¸ã®ããã«è¡¨ç¤ºããã¦ããã°OK $ echo $SHELL /bin/zsh
ããã¦ãEmacsãèµ·åãã¦ãhello.goãéãã¦ã¿ããfmt.Pã¾ã§å
¥åããã¨ä»¥ä¸ã®ããã«ä¿ç®¡ãããã
godocã使ããã
M-x godocã¨ãã¦ãfmt Printlnãã¨ããã
goflymake(https://github.com/dougm/goflymake)ã§ã¨ã©ã¼ãã§ãã¯ãªã©
å®ç¾©ã¸ã¸ã£ã³ãããã
Printlnã¸ã«ã¼ã½ã«ãæã£ã¦ãã¦ãC-c C-jãã§Printlnã®ã½ã¼ã¹ã³ã¼ãã¸ã¸ã£ã³ãããã
ã¨ããããä»æ¥ã¯ç°å¢è¨å®ã§çµããã次ã¯ãä½ãããã°ã©ã ãä½ã£ã¦ã¿ããããªã
追è¨ï¼2014/01/14ï¼
go-eldocã追å ï¼init.elã®è¨å®ã¯åè¿°ã®è¨å®ã«è¿½å ï¼
@syohex ããã«æãã¦ããã£ãgo-eldoc(GitHub - syohex/emacs-go-eldoc: eldoc for go language)ã追å ã以ä¸ã®ããã«ãããããã¡ã«ã¡ã½ããã®å¼æ°ãã§ã¦ããã(ã»âã»)イイ