LoginSignup
4

More than 5 years have passed since last update.

IntelliJ IDEAでGoのinterface実装を探しやすくする

Posted at

こちらの2014秋のGoConで鵜飼さんのキーノートにあるinterfaceのチェックを使うとすごく捗ります。
という話です。(IntelliJだと捗る話は最後に)

こんなinterfaceがあるとする

interface
type BotMessagePlugin interface {
    CheckMessage(ctx context.Context, message string) (bool, string)
    DoAction(ctx context.Context, message string) bool
}
interfaceの実装
type EchoMessage struct {
}

func (r EchoMessage) CheckMessage(ctx context.Context, message string) (bool, string) {
    return true, message
}

func (r EchoMessage) DoAction(ctx context.Context, message string) bool {
    plugins.SendMessage(ctx, message)
    return true // next ok
}

var _ plugins.BotMessagePlugin = (*EchoMessage)(nil)

interfaceのMethodを増やした場合

go buildでエラーになります。

Hoge()Mehtodを追加
type BotMessagePlugin interface {
    Hoge() bool
    CheckMessage(ctx context.Context, message string) (bool, string)
    DoAction(ctx context.Context, message string) bool
}

2015-06-26 7.16.08.png

interfaceのMethodの引数が変わった場合

これはinterfaceを使ってる箇所でも普通にbuildエラーになると思いますが。
未使用でもチェックできるようになります。

DoActionにhoge引数を追加
type BotMessagePlugin interface {
    CheckMessage(ctx context.Context, message string) (bool, string)
    DoAction(ctx context.Context, message string, hoge bool) bool
}

2015-06-26 7.20.17.png

IntelliJのDeclarationで実装がわかるようになる

ショートカットキーは、「Preferences」 -> 「keymap」でDeclarationを探してください。

スクリーンショット 2015-06-26 7.24.21.png

チェックを入れている箇所、interfaceを定義している箇所、interfaceを使用している箇所が一覧で出てきてかなり捗ります。
(もちろん選択してJump to Sourceできます)

おわり

IntelliJ完全に使いこなしているわけではないので、
他にも良い方法等ありましたら、コメント等いただけると嬉しいです。

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
4