Go の interface は、Java や PHP のように宣言で適用するインターフェイスを明示しません。
他の言語では、クラス宣言に implements キーワードなどを指定すると、IDE が必要なメソッドの仮実装を生成してくれるので便利でした。
GoLand でも似た機能が無いかなと思っていたら、type 宣言のタイプ名にカーソルを置いて、alt + enter
- Implement interface
を実行すると、実装したいインターフェイスを選択するダイアログが開き、選択すると interface を満たす仮実装を自動生成できました。
例えば、io.Reader
を選択すると、下記のような仮実装が生成されました。
type Foo struct {
}
// 自動生成
func (*Foo) Read(p []byte) (n int, err error) {
panic("implement me")
}
いちいち interface 宣言をコピペして実装したりしてたのですが、これは楽ですね。