This is a creation in Article, where the information may have evolved or changed.
Inspired by the Go Ultimate guide: Writing a go tool, the idea of encapsulating the gomodifytags into a emacs-plugin, and then a two-week effort, was born Emacs-go-tag. emacs-go-tag
implemented two commands: go-tag-add
add struct field tag, go-tag-remove
delete struct field tag.
Installation dependencies
emacs-go-tag
There are two dependencies, gomodifytags
and Go-mode.el, if using Emacs to do go development go-mode.el
should already be in use. Gomodifytags installation is as follows:
go get -u github.com/fatih/gomodifytags
Installation
Currently Emacs-go-tag has been published on Melpa and can be installed using the following command:
M-x package-install [RET] go-tag [RET]
If you do not find Go-tag, you need to use the following command to refresh, and then use the above command to install:
M-x package-refresh-contents [RET]
Configuration
gomodifytags
Three types of conversions are currently supported:
- Snakecase:basedomain-Base_domain
- Camelcase:basedomain-Basedomain
- Lispcase:basedomain-Base-domain
Default use snakecase
, if you are ready to use camelcase
, can be configured as follows:
(setq go-tag-args (list "-transform" "camelcase"))
Example of a shortcut key configuration:
(with-eval-after-load 'go-mode (define-key go-mode-map (kbd "C-c t") #'go-tag-add) (define-key go-mode-map (kbd "C-c T") #'go-tag-remove))
Instructions for use
If you have experience using Vim-go GoAddTags
and GoRemoveTags
commands, then you will be very quick to get started emacs-go-tag
because the two behaviors are exactly the same (if different, please feedback).
Go-tag-add
:[range] go-tag-add [key],[option] [key1],[option] …
Add a tag to the struct field. If called in a struct, the JSON field tag is added automatically. If it is called outside the struct, or if the file is malformed, an error message is given.
If given [range]
, only the selected field will be changed.
The default JSON can be changed by providing one or more [key]
parameters. Examples of adding XML and DB are:
:go-tag-add xml db
If provided, the tag [option]
will be added together, option
or the existing tag can be modified. As follows:
:go-tag-add json,omitempty
You can also define a constant value instead of the default field-based value. For example, the following command adds ' valid: ' 1 ' to all fields.
:go-tag-add valid=1
Go-tag-remove
:[range] go-tag-add [key],[option] [key1],[option] …
Removes the field marker from the structure field. If called in a struct, it will automatically delete all the field tags. If called outside of the struct definition, or if the file is malformed, an error message is given.
If given [range]
, only the selected field will be changed.
If given [key]
, only those key-related tags will be deleted. Only JSON-related tags are removed as follows:
:go-tag-remove json
If it is [option]
[key]
passed together, it will only be deleted option
. For example, the following command removes the Omitempty option only from fields that contain JSON:
:go-tag-remove json,omitempty
At last
Thanks to GoCN每日新闻
The daily push, I was able to read the Go Ultimate guide: Write a go tool and get this done emacs-plugin
. Project Address: Https://github.com/brantou/em, welcome to use and could be free questions, if you feel that it helps you 就给颗星
.