Setting up Emacs's Go programming language environment

Source: Internet
Author: User
Tags function definition goto

About Emacs does not explain, directly cut into the subject. For my Emacs configuration, you can refer directly to the lienhua34/myemacs-conf on GitHub.

Go-mode Installation

For Go-mode installation, you can refer to GitHub directly. Download the Go-mode.el or Go-mode-autoloads.el file from GitHub to one of your local directories, and then enter the following in the ~/.emacs file:

(Add-to-list ' Load-path "/path/to/your/dir") (Require ' go-mode-autoloads)

If you want to use Go-mode's new, make sure you have the Godef tool installed in your native environment first. If not, run the following command (and, of course, make sure you have the GO environment and set the GOPATH environment variable):

Go get github.com/rogpeppe/godef

Below is a description of the features provided by Go-mode.

Go-mode features Simple description 1 function signature processing

Go-mode provides a way to handle the function signature, which supports anonymous functions, but can also easily skip your life function. Go-mode provides the following function signature Processing (the default shortcut key in parentheses):

    • Go-goto-arguments (C-c c-f a): Jumps to the function parameter list;
    • Go-goto-docstring (c-c c-f D): Jumps to the docstring of the function, if the current function does not have docstring, it will automatically create a;
    • Go-goto-function (C-c c-f f): jumps to keyword func;
    • Go-goto-function-name (c-c c-f N): Jumps to the function name;
    • Go-goto-return-values (c-c c-f R): Jumps to the function return value;
    • Go-goto-method-receiver (C-c c-f m): jumps to the receiver of the function and automatically creates a heap of parentheses if the current function is not present.

You can skip the anonymous function by prefixing the above method or shortcut key with the c-u prefix.

2 formatting code

The go language itself provides the code for the GOFMT tool to format the go language. The Go-mode of Emacs uses the GOFMT tool to format the code that is being written. It is available in two ways:

    • Run the GOFMT function: M-x gofmt
    • Add a pre-save hook to automatically call Gofmt before saving the file for formatting.

Add the following code to the ~/.emacs configuration file to set the pre-save hooks,

"gofmt-before-save")

This way, when you save the Go language code file, you call GOFMT to format the contents of the code.

3 Viewing document Descriptions

The Go-mode combined with the Godoc tool provides docstring instructions for viewing a package or an identifier.

    • M-x Godoc: View the package's document description and prompt you to enter the package name
    • M-x godoc-at-point: docstring Description of the identifier to see where the current cursor is located
4 Administration of Import

Go-mode provides three ways to manage import:

    • Go-goto-imports (c-c c-f i): Jump to the import of the file;
    • Go-import-add (C-c c-a): Add import of New package, if shortcut key plus c-u prefix will prompt to import the package's nickname;
    • Go-remove-unused-imports: Delete or comment out unused import.

The Go-remove-unused-imports method does not have a binding accelerator by default, and the following code binds the method to the shortcut key "C-c c-r" in the ~/.emacs file.

' Go-mode-hook (Lambda ()                         'go-remove-unused-imports))
5 Code browsing

Go-mode provides some of the standard code-browsing shortcut keys, such as:

    • Beginning-of-defun (C-M-A): The cursor moves quickly to the beginning of the function definition;
    • End-of-defun (C-M-E): The cursor moves quickly to the end of the function definition;
    • Mark-defun (c-m-h): Quickly marks the entire contents of the current function;
    • Narrow-to-defun (c-x n d): View only the contents of the current function;

In addition, the Go-mode combines the Godef tool with the following three features:

    • Godef-describe (c-c c-d): View the description of the identifier (what type or its docstring description) at the current cursor location;
    • Godef-jump (c-c c-j): Jumps to the definition of the identifier where the cursor is located (in the current window);
    • Godef-jump-other-window (c-x 4 c-c c-j): Jumps to the definition of the identifier where the current cursor is located and opens in a new window.

Go-mode provides this jump can also jump to the standard library of the Go language, as shown below, move the cursor to the md5inst.sum () of the left buffer on the Sum identifier, and then press the shortcut key C-x 4 c-c c-j on the right to open the definition of the Sum () function,

Goflymake

If you're not sure what to Flymake, check out the GNU Flymake (rarely, also very simple, a few minutes to read).

Goflymake is Doug MacEachern's flymake-mode for the Go language, which is minor-mode for the Go language code to perform on-the-fly grammar checks.

Installation

For GitHub, first run the following command to install Goflymake:

Go get-u github.com/dougm/goflymake

Then add the following configuration in the ~/.emacs configuration file,

(Add-to-list ' Load-path "~/gocode/src/github.com/dougm/goflymake") (Require ' Go-flymake)

The three point in time at which the check is performed is,

    • After the buffer is loaded;
    • Change lines;
    • Content that was modified before 0.5s.
Use

Goflymake provides the following methods:

    • Flymake-mode: Close or open goflymake mode;
    • Flymake-start-syntax-check: Perform a grammar check;
    • Flymake-stop-all-syntax-checks: Stop all grammar checks;
    • Flymake-goto-next-error (c-c c-e N): Jumps to the next error point;
    • Flymake-goto-prev-error (C-c c-e p): Jumps to the previous error point;
    • Flymake-popup-current-error-menu (C-c c-e m): Displays the description of the current error point in popup mode.

By default, these methods do not have a binding shortcut key, and the following accelerator bindings are added to the ~/.emacs profile.

(Add-hook ' Flymake-mode-hook      (Lambda ()        (Local-set-key (kbd "c-c c-e n") ' Flymake-goto-next-error)]) (Add-hook ' Flymake-mode-hook      (Lambda ()        (Local-set-key (kbd "C-c c-e P") ' Flymake-goto-prev-error)) (Add-hook ' Flymake-mode-hook      (Lambda ()        (Local-set-key (kbd "C-c c-e M") (Flymake-popup-current-error-menu)))

The following (left is the result of the Goflymake check, the red statement is the problem, the right is the cursor moves to the first error point, pressing the shortcut key C-c c-e m after the error menu):

Reference list:

http://dominik.honnef.co/posts/2013/03/emacs-go-1/

Https://github.com/dominikh/go-mode.el

Https://github.com/dougm/goflymake

*******************

Welcome reprint, but please indicate the source OH * * *

*******************

Setting up Emacs's Go programming language environment

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.