1

Below is my emacs init file that relates to Go. I'm trying to use company mode to auto completes Go.

;;===================================================
; Golang related configuration
;;===================================================
(defun my-go-mode-hook ()
  ; Add go-guru
  (require 'go-guru)

  (require 'company)
  (require 'company-go)

  ; Use goimports instead of go-fmt
  (setq gofmt-command "goimports")
  ; Call Gofmt before saving
  (add-hook 'before-save-hook 'gofmt-before-save)
  ; Customize compile command to run go build
  (if (not (string-match "go" compile-command))
      (set (make-local-variable 'compile-command)
           ;"go build -v && go test -v && go vet"))
           "go run"))
  ; Godef jump key binding
  (local-set-key (kbd "M-.") 'godef-jump))
(add-hook 'go-mode-hook 'my-go-mode-hook)
; Highlight identifiers in Golang by default
(add-hook 'go-mode-hook #'go-guru-hl-identifier-mode)

However, it seems that the company mode can only completes words that have been used in the buffer. See below screen shot.

enter image description here

What I want is to complete Go's builtin functions, like fmt.Fprintln. It seems company mode can't do that. See below screen shot.

enter image description here

I want to confirm, is it true? Can emacs auto completes Go's builtin functions? Are there any example emacs init files that I can use as a reference? Thanks in advance.


Update

Confirmed I installed gocode.

enter image description here

I've cleared my init.el and only contains the minimal code to do the completion, but it still not work (emacs restarted).

enter image description here


Update2

Yes gocode is running. enter image description here

1 Answer 1

6

You need to tell company-mode what backend to use. In my init file I have the following code

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

You will also need the gocode autocomplete daemon which you can get by executing go get github.com/nsf/gocode.

8
  • hi Tephra, so all I need to do is add the code you pasted in order to get completion? Do I need to start gocode in a separate terminal? Does it work in terminal emacs, which is what I'm using. Commented Sep 4, 2016 at 8:29
  • Also do I need to press any key in order to trigger the completion? Or company will show it automatically? Commented Sep 4, 2016 at 8:30
  • When I type fmt. company-mode shows me all exposed methods that I can use without me having to press any key. You probably have to do a go get github.com/nsf/gocode also now that I come to think about it. I've updated my answer to reflect this.
    – Tephra
    Commented Sep 4, 2016 at 8:34
  • I already have gocode installed at my $GOPATH\bin. I tried your suggestion and only include the code you provided in my init file and it still not work. See my update image in my question. Commented Sep 4, 2016 at 8:39
  • 1
    I think I found the reason. I guess it's because for some unknown reason gocode has been started and hung there. I checked gocode doc and found a command gocode close to properly shut down gocode. Then suddenly it works. Thank you very much for your help! Commented Sep 4, 2016 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.