0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【GoLand】LiveTemplate で構造体に GORM タグをさくっとつける

Last updated at Posted at 2020-09-12

TL;DR

Preferences > LiveTemplates > Go Struct Tags からテンプレートを追加でできる。

goland_livetemplate.gif

設定

GoLand で、 Struct を定義する際、json とかいて json タグを自動で記述できるのはすごくべんりです。
gorm で列名を指定するとき、同じように記述するにはどうするかというはなし。

なお、例として gorm なだけで、それ以外でも同じ要領でもちろんだいじょうぶです。
ではやっていきます。

1.png


Preferences の LiveTemplates より、 Go Struct Tags を選択。
でテンプレートを追加します。

Abbreviation に gorm 、これは json みたいなテンプレートの起点になる文字です。
Description は適当に、 gorm:"column:xxx" とか。
Template text に値を、

`gorm:"column:$FIELD_NAME$"$END$`

のようにします。
変数も埋めれます。
変数はこの段階で未定義でだいじょうぶです。

Edit variables で変数を定義します。
値として Expression を入力します。

22.png


json のところを参考に snakeCase(fieldName()) にしています。
Expression を編集しようとするとサジェストしてくれるので、使えるものはここでわかります。

3.png


あとは、この LiveTemplate を適用する範囲を指定します。
Template text の下あたりに、 No application context とあるので、Define をクリック。
ここでは Go の Tag をチェックします。

これだけ。

参考

GoLand デフォルトで使用可能なLive Templates一覧 - Qiita

おわり。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?