gocode+auto-complete搭建emacs的go语言自动补全功能
下面是我的配置,写给自己看的
(add-to-list 'load-path "/home/wangjunsheng/go/emacs/go-mode")
(require 'go-mode)
(define-key go-mode-map [(meta ?g)] 'godef-jump)
(custom-set-variables '(gofmt-command "goimports"))
(define-key go-mode-map "\C-c\C-f" 'gofmt)
(add-to-list 'load-path "/home/wangjunsheng/.emacs.d")
(require 'go-autocomplete)
(require 'auto-complete-config)
(ac-config-default)
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)
如果 godef 无法跳转 可以加入 -debug 参数看看 原因
比如下面是成功的
godef -debug -f root_run.go mqttpubsub.NewBackend
2019/02/20 15:45:18 exprType tuple:false pkg: *ast.SelectorExpr mqttpubsub.NewBackend [
2019/02/20 15:45:18 exprType tuple:false pkg: *ast.Ident mqttpubsub [
2019/02/20 15:45:18 exprType tuple:false pkg: *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub" [
2019/02/20 15:45:18 ] -> 0x0, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:45:18 ] -> 0x1a6004b0, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:45:18 member Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"} 'NewBackend' {
2019/02/20 15:45:18 } -> &{func NewBackend 0x1a5619e0 <nil> <nil>}
2019/02/20 15:45:18 exprType tuple:false pkg: *ast.Ident NewBackend [
2019/02/20 15:45:18 exprType tuple:false pkg: *ast.FuncType func(c newMQTT.BackendConfig) (*Backend, error) [
2019/02/20 15:45:18 ] -> 0x0, Type{type "" *ast.FuncType func(c newMQTT.BackendConfig) (*Backend, error)}
2019/02/20 15:45:18 ] -> 0x1a530330, Type{func "" *ast.FuncType func(c newMQTT.BackendConfig) (*Backend, error)}
2019/02/20 15:45:18 ] -> 0x1a530330, Type{func "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub" *ast.FuncType func(c newMQTT.BackendConfig) (*Backend, error)}
/home/wangjunsheng/go/gopath/src/github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub/backend.go:38:6
再比如下面这个不成功的例子
godef -debug -f root_run.go mqttpubsub.NewBackend
2019/02/20 15:23:05 exprType tuple:false pkg: *ast.SelectorExpr mqttpubsub.NewBackend [
2019/02/20 15:23:05 exprType tuple:false pkg: *ast.Ident mqttpubsub [
2019/02/20 15:23:05 exprType tuple:false pkg: *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub" [
2019/02/20 15:23:05 ] -> 0x0, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:23:05 ] -> 0x194f8000, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:23:05 member Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"} 'NewBackend' {
2019/02/20 15:23:06 /home/wangjunsheng/go/gopath/src/github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub/metrics.go:7:2: cannot find identifier for package "github.com/prometheus/client_golang/prometheus": cannot find package "github.com/prometheus/client_golang/prometheus" in any of:
/home/wangjunsheng/go/go/src/github.com/prometheus/client_golang/prometheus (from $GOROOT)
/home/wangjunsheng/go/gopath/src/github.com/prometheus/client_golang/prometheus (from $GOPATH)
2019/02/20 15:23:06 } -> <nil>
2019/02/20 15:23:06 ] -> 0x0, Type{bad "" <nil> }
2019/02/20 15:23:06 exprType tuple:false pkg: *ast.SelectorExpr mqttpubsub.NewBackend [
2019/02/20 15:23:06 exprType tuple:false pkg: *ast.Ident mqttpubsub [
2019/02/20 15:23:06 exprType tuple:false pkg: *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub" [
2019/02/20 15:23:06 ] -> 0x0, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:23:06 ] -> 0x194f8000, Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"}
2019/02/20 15:23:06 member Type{package "" *ast.ImportSpec "github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub"} 'NewBackend' {
2019/02/20 15:23:06 /home/wangjunsheng/go/gopath/src/github.com/brocaar/lora-gateway-bridge/internal/legacy/backend/mqttpubsub/metrics.go:7:2: cannot find identifier for package "github.com/prometheus/client_golang/prometheus": cannot find package "github.com/prometheus/client_golang/prometheus" in any of:
/home/wangjunsheng/go/go/src/github.com/prometheus/client_golang/prometheus (from $GOROOT)
/home/wangjunsheng/go/gopath/src/github.com/prometheus/client_golang/prometheus (from $GOPATH)
2019/02/20 15:23:06 } -> <nil>
2019/02/20 15:23:06 ] -> 0x0, Type{bad "" <nil> }
godef: no declaration found for mqttpubsub.NewBackend
借助日志,我们可以看到是因为没有相应的 go 第三方库导致的。 下载这些库可以解决问题