go koan をしようとして、interface(struct) 構文を理解するのに行き詰まりました。正確には何をしますか? 次の楽しいプログラムを思いつきました。これにより、インターフェイスのキャストがどのように機能するかについてさらに混乱しました。
package main
import "fmt"
type foo interface{ fn() }
type t struct { }
type q struct { }
func (_i t ) fn() { fmt.Print("t","\n") }
func (_i q ) fn() { fmt.Print("q","\n")}
func main() {
_j := t{}
_q := q{}
// This is alright ..
fmt.Print( _j.fn,"\n") //0x4015e0
fmt.Print( _q.fn,"\n") //0x401610
_j.fn() //t
_q.fn() //q
// both pointers same .. why ?
fmt.Print( foo(_j).fn,"\n") //0x401640
fmt.Print( foo(_q).fn,"\n") //0x401640
// but correct fns called .. how ?
foo(_j).fn() //t
foo(_q).fn() //q
// same thing again ...
_fj := foo(_j).fn
_fq := foo(_q).fn
// both pointers same .. as above
fmt.Print( _fj,"\n") //0x401640
fmt.Print( _fq,"\n") //0x401640
// correct fns called .. HOW !
_fj() //t
_fq() //q
}
ポインターは、私が自分のマシン、YMMV を取得しているものです。私の質問は.. interface(struct) は正確に何を返しますか? そして、 interface(struct). func はどのように元の構造体を見つけますか...ここでサンク/スタブマジックが行われていますか?