5
2

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 で interface を満たす仮実装を自動生成

Last updated at Posted at 2018-08-09

Go の interface は、Java や PHP のように宣言で適用するインターフェイスを明示しません。

他の言語では、クラス宣言に implements キーワードなどを指定すると、IDE が必要なメソッドの仮実装を生成してくれるので便利でした。

GoLand でも似た機能が無いかなと思っていたら、type 宣言のタイプ名にカーソルを置いて、alt + enter - Implement interface を実行すると、実装したいインターフェイスを選択するダイアログが開き、選択すると interface を満たす仮実装を自動生成できました。

a.png

例えば、io.Reader を選択すると、下記のような仮実装が生成されました。

type Foo struct {
}

// 自動生成
func (*Foo) Read(p []byte) (n int, err error) {
	panic("implement me")
}

いちいち interface 宣言をコピペして実装したりしてたのですが、これは楽ですね。

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?