Auto-complete with Go-mode in Emacs

2,677 views
Skip to first unread message

Tahir Hashmi

unread,
Sep 6, 2012, 1:08:52 AM9/6/12
to golan...@googlegroups.com
I've been trying to get auto-complete-mode to be activated by default with go-mode in Emacs, but my efforts so far have been futile. More details on this question at stackoverflow: http://stackoverflow.com/questions/12278990/auto-complete-with-go-mode

I'm hoping someone on this list might be able to answer it better. This issue is killing my productivity, not because I can't write code without auto-completion but because I can't get over the fact that something so simple should result in so much pain. ;-)

TIA

-- 
Tahir Hashmi

Raph

unread,
Sep 17, 2012, 2:55:33 AM9/17/12
to golan...@googlegroups.com
works for me just fine, too. Only problem I had setting it up was that gocode wasn't in my GOPATH.

Here's the relevant part of my Emacs init.el (from my windows box)

(add-to-list 'load-path "c:/documents and settings/me/Application Data/.emacs.d")

(require 'go-mode-load)

(require 'go-autocomplete)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d//ac-dict")
(ac-config-default)

Jon Hirschtick

unread,
Sep 19, 2012, 12:55:44 AM9/19/12
to golan...@googlegroups.com
Works for me on my Mac (Gnu Emacs 24.2.1 built for Cocoa), but have to start it from a Terminal window:

$ open -a emacs

My ~/.emacs looks a lot like the other examples posted.

Good luck,

- Jon

Meir

unread,
Sep 19, 2012, 8:54:30 AM9/19/12
to golan...@googlegroups.com

In my Emacs, auto-complete mode for Go works in part. That is, it gives me choices for completing names of functions from packages (and even shows their signatures), but does not complete names of variables, types etc. that I declared in my code... 

Kevin Gillette

unread,
Sep 19, 2012, 12:57:37 PM9/19/12
to golan...@googlegroups.com
gocode should handle the local package, but anything from imports that are not `go install`ed (or in turn, fetched through`go get`) won't be autocompleted

Meir Goldenberg

unread,
Sep 19, 2012, 1:21:52 PM9/19/12
to Kevin Gillette, golan...@googlegroups.com

When I programmed in C using Emacs, all of the identifiers that I defined were auto-completed. Is there a good reason why the same should not be the case with Go? For example, if I have just declared a local variable, why shouldn't it be auto-completed when I want to use it later in the same scope?

On Wed, Sep 19, 2012 at 7:57 PM, Kevin Gillette <extempor...@gmail.com> wrote:
gocode should handle the local package, but anything from imports that are not `go install`ed (or in turn, fetched through`go get`) won't be autocompleted

--



DisposaBoy

unread,
Sep 19, 2012, 2:17:51 PM9/19/12
to golan...@googlegroups.com, Kevin Gillette
I don't know why it doesn't work in Emacs but I can tell you for a fact that it's not a problem with gocode. The Emacs plugin most likely doesn't call gocode correctly

nsf

unread,
Sep 19, 2012, 3:01:35 PM9/19/12
to golan...@googlegroups.com
On Wed, 19 Sep 2012 20:21:52 +0300
Meir Goldenberg <mgol...@gmail.com> wrote:

> When I programmed in C using Emacs, all of the identifiers that I defined
> were auto-completed. Is there a good reason why the same should not be the
> case with Go? For example, if I have just declared a local variable, why
> shouldn't it be auto-completed when I want to use it later in the same
> scope?

Emacs plugin perhaps restricts autocompletion on members only. For
auto-complete-mode there is a "prefix" definition in the ac-source
implementation. I define it to a '.' symbol.

You're free to make a better emacs plugin for gocode, gocode itself
supports local scope autocompletion as DisposaBoy noted. I'm not really
interested in quality of the plugins which bundled with gocode itself.
They do the job for me and perhaps for some other people. But
maintaining them - yuck (I'm also slowly moving away from emacs and
vim towards my own text editor).


Sincerely yours, the gocode author.

Meir Goldenberg

unread,
Sep 21, 2012, 5:49:09 AM9/21/12
to nsf, golan...@googlegroups.com

I do not know lisp. I found the following line of code in go-autocomplete.el:

(defvar ac-source-go
  '((candidates . ac-go-candidates)
    (prefix . "\\.\\(.*\\)")
    (requires . 0)))

Should I write this instead:

(defvar ac-source-go
  '((candidates . ac-go-candidates)
    (prefix . '.')
    (requires . 0)))
?

Could you please point me exactly to the file and line number that I need to modify and spell out the change?
Thank you!
Meir



--



nsf

unread,
Sep 21, 2012, 3:37:31 PM9/21/12
to Meir Goldenberg, golan...@googlegroups.com
On Fri, 21 Sep 2012 12:49:09 +0300
Meir Goldenberg <mgol...@gmail.com> wrote:

> I do not know lisp. I found the following line of code in
> go-autocomplete.el:
>
> (defvar ac-source-go
> '((candidates . ac-go-candidates)
> (prefix . "\\.\\(.*\\)")
> (requires . 0)))
>
> Should I write this instead:
>
> (defvar ac-source-go
> '((candidates . ac-go-candidates)
> (prefix . '.')
> (requires . 0)))
> ?
>
> Could you please point me exactly to the file and line number that I need
> to modify and spell out the change?
> Thank you!
> Meir

I have no idea how to fix it.

Meir Goldenberg

unread,
Sep 22, 2012, 1:47:39 PM9/22/12
to pbgc...@gmail.com, golan...@googlegroups.com

I am quite frustrated. Even the functionality that I described as working is not working every time... The suggested change did not help.
I would very much appreciate if someone could please describe from scratch what they did to get things to work.
Just in case this might help, the relevant files of mine are attached to this message.
Also, here is the output of some relevant commands:

 meir@meir-desktop:~/CurrentWork/Go/bin$ echo $GOPATH
/home/meir/CurrentWork/Go/:/home/meir/CurrentWork/Go/bin

meir@meir-desktop:~/CurrentWork/Go/bin$ ls /home/meir/CurrentWork/Go/bin
gocode

meir@meir-desktop:~/CurrentWork/Go/bin$ ls ~/.emacs.d/go-autocomplete.el
/home/meir/.emacs.d/go-autocomplete.el

On Sat, Sep 22, 2012 at 6:38 PM, <pbgc...@gmail.com> wrote:
I do it by changing in go-autocomplete.el the line:

(setq ac-sources '(ac-source-go))

with:

(setq ac-sources (append ac-sources '(ac-source-go))

It works for me...
.emacs
go-autocomplete.el

Luke Mauldin

unread,
Oct 30, 2012, 4:30:00 PM10/30/12
to golan...@googlegroups.com, kitsi...@gmail.com
Was a solution ever found to this problem?  I am working on this as well and it works fine if I do {struct}. and then it gives me the members.  However, if I have a function local to the package I have to type . in order to get an auto-completion list.  Is there any way that I can change this or maybe type "Ctrl-Tab", etc... to get the auto complete list?

Luke

On Friday, October 26, 2012 1:27:26 PM UTC-5, kitsi...@gmail.com wrote:
me too. i don't know why need to start emacs from terminal window.

thanks

morozo...@gmail.com

unread,
Sep 8, 2016, 2:17:18 AM9/8/16
to golang-nuts, kitsi...@gmail.com
I know this is about 4 years late, however reading through the docs I found out that you need to run go install on the "main" package to get auto-complete to recognize all the packages that are part of the file you are working on. it can be found here https://github.com/nsf/gocode#misc

basically to build out the tree and force auto-complete to build its tree and dictionary I guess it needs this command, so whenever you create a new package and import it into main you will need to run go install for it to register

-- again this forum seems dead but hopefully someone in need will find this info useful. (this is for auto-complete and go-eldoc)
Reply all
Reply to author
Forward
0 new messages