emacs-go-tag icon indicating copy to clipboard operation
emacs-go-tag copied to clipboard

Edit field tags for golang struct fields

#+TITLE: emacs-go-tag [[http://www.gnu.org/licenses/gpl-3.0.html][file:https://img.shields.io/badge/license-GPL_v3-green.svg]] [[https://melpa.org/#/go-tag][file:https://melpa.org/packages/go-tag-badge.svg]] [[https://stable.melpa.org/#/go-tag][file:https://stable.melpa.org/packages/go-tag-badge.svg]]

  • Introduction :PROPERTIES: :ID: 433a1d5f-9353-496f-a783-8c123cc0a633 :END:

    Edit field tags for golang struct fields, based on [[https://github.com/fatih/gomodifytags][gomodifytags]]. This package is inspired by ~GoAddTags~ of [[https://github.com/fatih/vim-go][vim-go]] and [[https://github.com/syohex/emacs-go-add-tags][go-add-tags]].

  • gomodifytags :PROPERTIES: :ID: f51e9e98-85ed-428a-90ba-d0727a257402 :END: Go tool to modify/update field tags in structs. ~gomodifytags~ makes it easy to update, add or delete the tags in a struct field. It can be installed with #+BEGIN_SRC shell :eval strip-export go get github.com/fatih/gomodifytags #+END_SRC

  • Installation :PROPERTIES: :ID: edb6d6eb-9d05-418c-81d9-7452b9db3d69 :END:

Available on all major ~package.el~ community maintained repos - [[https://stable.melpa.org/#/][MELPA Stable]] and [[https://melpa.org/#/][MELPA]] repos.

MELPA Stable is recommended as it has the latest stable version. MELPA has a development snapshot for users who don't mind breakage but don't want to run from a git checkout.

You can install ~go-tag~ using the following command:

~M-x package-install [RET] go-tag [RET]~

or if you'd rather keep it in your dotfiles:

#+BEGIN_SRC elisp (unless (package-installed-p 'go-tag) (package-refresh-contents) (package-install 'go-tag)) #+END_SRC

If the installation doesn't work try refreshing the package list:

~M-x package-refresh-contents~

  • Commands :PROPERTIES: :ID: dbee016c-6edd-4999-9303-419d35469ad2 :END: If you are familiar with ~GoAddTags~ AND ~GoRemoveTags~ of [[https://github.com/fatih/vim-go][vim-go]] , ~go-tag~ you will get started quickly.

    • ~go-tag-add~ #+BEGIN_QUOTE :[range]go-tag-add [key],[option] [key1],[option] ... #+END_QUOTE

      Adds field tags for the fields of a struct. If called inside a struct it automatically add field tags with the ~json~ key and the value automatically generated based on the field name. An error message is given if it's called outside a struct definition or if the file is not correctly formatted.

      If [range] is given, only the selected fields will be changed.

      The default ~json~ can be changed by providing one or more [key] arguments. An example of adding ~xml~ and ~db~ would be:

      #+BEGIN_QUOTE :go-tag-add xml db #+END_QUOTE

      If [option] is passed it'll either add a new tag with an option or will modify existing tags. An example of adding ~omitempty~ to all ~json~ fields would be: #+BEGIN_QUOTE :go-tag-add json,omitempty #+END_QUOTE

      You can define a constant value instead of the default field based value. For example the following command will add ~valid:"1"~ to all fields. #+BEGIN_QUOTE :go-tag-add valid=1 #+END_QUOTE

      [[https://github.com/brantou/emacs-go-tag/blob/master/img/go-tag-add.gif]]

    • ~go-tag-remove~

      #+BEGIN_QUOTE :[range]go-tag-remove [key],[option] [key1],[option1] ... #+END_QUOTE

      Rmove field tags for the fields of a struct. If called inside a struct it automatically remove all field tags. An error message is given if it's called outside a struct definition or if the file is not correctly formatted

      If [range] is given, only the selected fields will be changed.

      If [key] is given, it will only remove those keys. Example:

      #+BEGIN_QUOTE :go-tag-remove json #+END_QUOTE

      If [option] is passed with a [key], it will only remove the options. Example, this will only remove ~omitempty~ options from fields containing ~json~:

      #+BEGIN_QUOTE :go-tag-remove json,omitempty #+END_QUOTE [[https://github.com/brantou/emacs-go-tag/blob/master/img/go-tag-remove.gif]]

    • ~go-tag-refresh~ (Useful when designing structures) #+BEGIN_QUOTE :[range]go-tag-refresh [key],[option] [key1],[option] ... #+END_QUOTE

      Equal to: #+BEGIN_QUOTE :[range]go-tag-remove [key] [key1] ...

      :[range]go-tag-add [key],[option] [key1],[option] ... #+END_QUOTE

  • Configuration :PROPERTIES: :ID: 9f364afb-69ae-47dc-ae2e-d76bdcefc928 :END:

    support the following transformations:

    • ~snakecase~: ~BaseDomain~ -> ~base_domain~
    • ~camelcase~: ~BaseDomain~ -> ~baseDomain~
    • ~lispcase~: ~BaseDomain~ -> ~base-domain~

    #+BEGIN_SRC elisp :eval strip-export (setq go-tag-args (list "-transform" "camelcase")) #+END_SRC

    #+BEGIN_SRC elisp :eval strip-export (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)) #+END_SRC