How to configure multiple Golang projects in Emacs

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Overview

When you recently developed a project using Golang, you found that you sometimes need to do multiple Golang projects at once.
In this case, if you put all the items under Gopath,
Not only does it manage trouble (because projects need to be submitted to different code repositories), and it's not convenient to compile (if you don't specify a project, all projects are compiled).

As a result, you want to be physically separated from each project. I developed the Golang project with Emacs, so after the isolation, also consider how to make Emacs Golang plug-ins to work properly.

Isolate individual Golang projects with GB

GB is a tool for managing Golang projects, and using GB, you can create Golang projects in any location, not necessarily in Gopath.
GB official address has detailed instructions, mainly on a few parameters, easy to use.

Installation

go get github.com/constabulary/gb/...

GB installed in Gopath, after installation, if your $GOPATH/bin in the system $PATH, the command line can use the GB command.

Introduction to use

Create Project: http://getgb.io/docs/project/
Parameters of the GB command: http://getgb.io/docs/usage/
Simple way to use: http://getgb.io/examples/getting-started/

Settings for Gopath

The Gopath set in the system is not automatically recognized in Emacs, so the GOPATH environment variable must be set in Emacs.
The Setup method is as follows:

(setenv "GOPATH" (concat ""                       "/path/to/PROJECTA:"                       "/path/to/PROJECTA/vendor:"                       "/path/to/GOPATH"))

In the example above, the path to my project ProjectA is added to Gopath, and the vendor folder is generated when a third-party package is installed by the GB tool.
If you have more than one project, you can also configure the path of multiple projects into Gopath.

Go-oracle Scope settings

When the above Gopath is set correctly, Emacs and many Golang tools can be used, such as goimports, GODEF, etc.
Using these tools, you can implement function-defined jumps, automatically import the package, and so on, making the editor more like an IDE.

However, if you want to use Go-oracle, only Gopath settings are not yet set Go-oracle-scope
Go-oracle Document: Https://godoc.org/golang.org/x/tools/cmd/oracle
Go-oracle settings can be consulted: http://yousefourabi.com/blog/2014/05/emacs-for-go/
In this document, Go-oracle's download path is old, and the current location for installing Oracle is as follows:

go get golang.org/x/tools/cmd/oracle

Go-oracle Common shortcut keys:

C-c C-o <       go-oracle-callersC-c C-o >       go-oracle-calleesC-c C-o c       go-oracle-peersC-c C-o d       go-oracle-definitionC-c C-o f       go-oracle-freevarsC-c C-o g       go-oracle-callgraphC-c C-o i       go-oracle-implementsC-c C-o p       go-oracle-pointstoC-c C-o r       go-oracle-referrersC-c C-o s       go-oracle-callstackC-c C-o t       go-oracle-describe

Like Go-oracle-scope and Gopath, you can set multiple, which is set in the package unit.
You can just set up a package, such as:

(setq go-oracle-scope "lib/log")

Then, when running go-oracle-callers, it will find the package (Lib/log) in the previously defined Gopath, and then parse it.
You can then use the shortcut keys above to query the various analysis results of the functions in this package.

You can also specify the location of the main function in this project, so that Oracle analyzes the entire project, not a package in the project, such as:

(setq go-oracle-scope "xxcmd")

As set above, Oracle will
/path/to/projecta/src/xxxcmd/path/to/projecta/vendor/src/xxxcmd/path/to/gopath/src/xxxcmd
Find the package in 3 paths and analyze it.

All configurations together

The above configuration is simple and can be configured directly in. emacs.
But I found in the use, if the number of items, first, gopath in the configuration path too many, each time in the Go-oracle analysis will be very slow.
And if the package name is the same for multiple projects in Gopath, for example, ProjectA and PROJECTB have lib/ssh libraries,
This library does not have the same implementation in ProjectA and PROJECTB, which contains different functions.
Then, Godef and the like will only be found in the first path in the Gopath, not find the error, and not go to each path in turn to find.

So, according to my situation, the following configuration is currently used:

    1. Create a new El file, such as Go-projects.el, to manage the configuration of multiple Golang projects

      touch /path/to/go-projects.el
    2. Set the Gopath in Go-projects.el

      (setenv "GOPATH" (concat ""                       "/path/to/PROJECTA:"                       "/path/to/PROJECTA/vendor:"                       "/path/to/PROJECTB:"                       "/path/to/PROJECTB/vendor:"                       "/path/to/GOPATH"))
    3. Append the scope configuration of the Go-oracle

      (setq go-oracle-scope "xxxcmd")
      Assuming that the main function is in/path/to/projectb/src/xxcmd
    4. . Emacs Load Go-projects.el

      (load "/path/to/go-projects.el")

During use, you can adjust the project path in the Gopath at any time in go-projects.el, without deleting or commenting on the items.
Go-oracle-scope can also be adjusted to suit the situation at any time.
After adjustment, the M+x Load-file/path/to/go-projects.el takes effect immediately.
This will not modify the. emacs file because the reload. Emacs is very slow and can even cause errors.

Golang currently manages a number of projects in a relatively small way, the above methods can only be used. For reference only! :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.