Go interfaceの引数について

実現したいこと

interfaceのメソッドに引数の概念を設定したい。
現状interfaceのPushメソッドを記述し、実態のメソッドでPush(s string)を設定すると定義の違いでエラーになってしまう。

発生している問題・エラーメッセージ

./prog.go:8:21: cannot use publish{…} (value of type publish) as publisher value in variable declaration: publish does not implement publisher (wrong type for method Push) have Push(string) want Push()

該当のソースコード

go

1package main 2 3import "fmt"4 5func main() {6 var ph publisher = publish{messege: "正義は"}7 ph.Push()8 9}10 11type publisher interface {12 Push()13}14 15type publish struct {16 messege string17}18 19func (p publish) Push(s string) {20 fmt.Println(p.messege + "勝つ")21}

補足情報(FW/ツールのバージョンなど)

Go 1.21

コメントを投稿

0 コメント