在Go教程的第2天,有以下练习:
为什么具有带有导出字段的私有类型可能很有用?
例如:
package geometry
type point struct {
X, Y int;
name string;
}
请注意,point
是小写的,因此不导出,而字段X和Y是大写的,因此不导出.在我看来,为了访问其中一个导出字段,您必须能够编写以下内容.
p.X
但要实现这一点,p必须有这样的声明:
var p geomitry.point;
或
p := new(geomitry.point);
This however is not possible (afaik), since the type declaration f或 point isn't exp或ted.