上級 › ポインタレシーバのメソッドしか無い型は値ではinterfaceを満たさない › パターン5
ポインタレシーバのメソッドしか無い型は値ではinterfaceを満たさない
ポインタレシーバで定義したメソッドは、その型の値そのもの(非ポインタ)のメソッドセットには含まれません。
1 package main 2 3 import "fmt" 4 5 type Painter interface { 6 Paint() string 7 } 8 9 type Wall struct{ Color string } 10 11 func (w *Wall) Paint() string { return "painted " + w.Color } 12 13 func main() { 14 w := Wall{Color: "blue"} 15 var p Painter = _______ ^ 16 fmt.Println(p.Paint()) 17 }
./main.go:15:18: cannot use w (variable of type Wall) as Painter value in variable declaration: Wall does not implement Painter (method Paint has pointer receiver)
空欄を埋めて実行を通す
この制約はコンパイル時に静的に検査されるため、実行前に気づくことができます。
次の問題