LoginSignup
11
5

More than 5 years have passed since last update.

Emacs x golang : company-goのハマりどころ

Last updated at Posted at 2017-02-27

結論

GOROOT , GOPATH はちゃんと環境変数に設定しましょう。

  • GOROOT: go をインストールした場所
  • GOPATH: go getgo install でパッケージがインストールされる場所

GOROOTGOPATH はサイトによってマチマチに書かれていたり、
設定しなくても良いようなことが書かれていたり、イマイチよくわからない。

けど、必要でした!!

前提

  • go 導入済み

company-go

以下を参考にしました。

https://github.com/nsf/gocode
https://github.com/nsf/gocode#setup
https://github.com/nsf/gocode#emacs-setup
https://github.com/nsf/gocode/tree/master/emacs-company

go言語の設定

GOPATH

go get などでインストールされるパッケージが保存されるディレクトリです。
通常は以下のように設定するようです。(http://golang-jp.org/doc/code.html#GOPATH)

export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH

個人的には .go ディレクトリにしたかったので以下の通りです。

export GOPATH=$HOME/.go
export PATH=$GOPATH/bin:$PATH

gocode

言われるがままインストール

go get -u github.com/nsf/gocode

Emacsの設定

必要なパッケージ

  • go-mode
  • company, company-go

上記のパッケージを package.el を使ってインストールしました。

設定ファイル

言われるがまま記述

go-mode.el
;;; Setup

(require 'company)                                   ; load company mode
(require 'company-go)                                ; load company mode go backend

;;; Possible improvements

(setq company-tooltip-limit 20)                      ; bigger popup window
(setq company-idle-delay .3)                         ; decrease delay before autocompletion popup shows
(setq company-echo-delay 0)                          ; remove annoying blinking
(setq company-begin-commands '(self-insert-command)) ; start autocompletion only after typing

;;; Only use company-mode with company-go in go-mode

(add-hook 'go-mode-hook (lambda ()
                          (set (make-local-variable 'company-backends) '(company-go))
                          (company-mode)))

;;; Color customization

(custom-set-faces
 '(company-preview
   ((t (:foreground "darkgray" :underline t))))
 '(company-preview-common
   ((t (:inherit company-preview))))
 '(company-tooltip
   ((t (:background "lightgray" :foreground "black"))))
 '(company-tooltip-selection
   ((t (:background "steelblue" :foreground "white"))))
 '(company-tooltip-common
   ((((type x)) (:inherit company-tooltip :weight bold))
    (t (:inherit company-tooltip))))
 '(company-tooltip-common-selection
   ((((type x)) (:inherit company-tooltip-selection :weight bold))
    (t (:inherit company-tooltip-selection)))))

いざ!

hello.go
package main

import "fmt"

func main() {
    fmt.P
}

でない・・・

色々なサイトを駆け巡り、

色々な設定を試し、

無意味に入っていた auto-complete パッケージを削除してみたが、

うーん・・・上手くいかない・・・。

辿り着いた Debugging

$ gocode close
$ gocode -s -debug

そのままEmacsでgo-modeを使っていると・・・

2017/02/27 14:08:46 =======================================================
2017/02/27 14:08:48 Go project path: .
2017/02/27 14:08:48 Got autocompletion request for '/home/chibi/tmp/hello.go'
2017/02/27 14:08:48 Cursor at: 48
2017/02/27 14:08:48 -------------------------------------------------------
package main

import "fmt"

func main() {
    fmt.P#
}
2017/02/27 14:08:48 -------------------------------------------------------
2017/02/27 14:08:48 Import path "fmt" was not resolved
2017/02/27 14:08:48 Gocode's build context is:
2017/02/27 14:08:48  GOROOT: /usr/local/go
2017/02/27 14:08:48  GOPATH: /home/chibi/.go
2017/02/27 14:08:48  GOOS: linux
2017/02/27 14:08:48  GOARCH: amd64
2017/02/27 14:08:48  BzlProjectRoot: ""
2017/02/27 14:08:48  GBProjectRoot: ""
2017/02/27 14:08:48  lib-path: ""
2017/02/27 14:08:48 extracted expression tokens: fmt
2017/02/27 14:08:48 Offset: 0
2017/02/27 14:08:48 Number of candidates found: 0
2017/02/27 14:08:48 Candidates are:
2017/02/27 14:08:48 =======================================================

GOROOTが違う。

$ which go
/home/chibi/.goenv/shims/go

$ go env GOROOT
/home/chibi/.goenv/versions/1.8

$ echo $GOROOT

実際はこんな感じだ。
go 自体は goenv を使って導入している。

とりあえず .bashrc に以下を追加

export GOROOT=~/.goenv/versions/1.8

無事、保管リストが表示されました。

20170227230322.png

しかし、このままだと goenv でインストールしたバージョンを変えたら、
GOROOTも変えないといけないので、シンボリックリンク等必要な感じ・・・。
goenv が上手くやってくれないのだろうか・・・

export GOROOT=`go env GOROOT`

とりあえず↑としてみた。

駆け巡った参考ページたち

http://qiita.com/bussorenre/items/3e80e59d517db8aebf46
http://unknownplace.org/archives/golang-editing-with-emacs.html
http://emacs-jp.github.io/programming/golang.html

ありがとうございました。

godef や go-eldoc, flycheck, golint では、特にハマってないので、
今回は company-go のみに絞った記事にしました。

11
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
5