草庐IT

PrintLocation

全部标签

go - 为什么具有匿名结构字段的结构不满足在该类型的别名上定义的方法?

我在我的包外定义了一个结构,我想附加一个方法。由于包反射(reflect)了原始类型,我不能在我的结构中使用别名,我必须使用原始类型。以下基本上是我正在尝试做的事情:packagemainimport"fmt"typeEntitystruct{loc_xintloc_yint}typePlayerstruct{Entitynamestring}typeAliasEntityfunc(eAlias)PrintLocation(){fmt.Printf("(%v,%v)",e.loc_x,e.loc_y)}funcmain(){player:=new(Player)player.Print

go - 为什么具有匿名结构字段的结构不满足在该类型的别名上定义的方法?

我在我的包外定义了一个结构,我想附加一个方法。由于包反射(reflect)了原始类型,我不能在我的结构中使用别名,我必须使用原始类型。以下基本上是我正在尝试做的事情:packagemainimport"fmt"typeEntitystruct{loc_xintloc_yint}typePlayerstruct{Entitynamestring}typeAliasEntityfunc(eAlias)PrintLocation(){fmt.Printf("(%v,%v)",e.loc_x,e.loc_y)}funcmain(){player:=new(Player)player.Print