匿名字段和内嵌结构体
结构体可以包含一个或多个 匿名(或内嵌)字段,即这些字段没有显式的名字,只有字段的类型是必须的,此时类型就是字段的名字。匿名字段本身可以是一个结构体类型,即 结构体可以包含内嵌结构体。
可以粗略地将这个和面向对象语言中的继承概念相比较,随后将会看到它被用来模拟类似继承的行为。Go 语言中的继承是通过内嵌或组合来实现的,所以可以说,在 Go 语言中,相比较于继承,组合更受青睐。
package main import "fmt" type innerS struct { in1 int in2 int } type outerS struct { b int c float32 int // anonymous field innerS // anonymous field } func main() { outer := new(outerS) outer.b = 6 outer.c = 7.5 outer.int = 60 outer.in1 = 5 outer.in2 = 10 fmt.Printf("outer.b is: %d ", outer.b) fmt.Printf("outer.c is: %f ", outer.c) fmt.Printf("outer.int is: %d ", outer.int) fmt.Printf("outer.in1 is: %d ", outer.in1) fmt.Printf("outer.in2 is: %d ", outer.in2) // with a struct-literal: outer2 := outerS{6, 7.5, 60, innerS{5, 10}} fmt.Println("outer2 is: ", outer2) }
通过类型 outer.int
的名字来获取存储在匿名字段中的数据,于是可以得出一个结论:在一个结构体中对于每一种数据类型只能有一个匿名字段。
同样地结构体也是一种数据类型,所以它也可以作为一个匿名字段来使用,如同上面例子中那样。外层结构体通过 outer.in1
直接进入内层结构体的字段,内嵌结构体甚至可以来自其他包。内层结构体被简单的插入或者内嵌进外层结构体。这个简单的“继承”机制提供了一种方式,使得可以从另外一个或一些类型继承部分或全部实现。
package main import "fmt" type A struct { ax, ay int } type B struct { A bx, by float32 } func main() { b := B{A{1, 2}, 3.0, 4.0} fmt.Println(b.ax, b.ay, b.bx, b.by) fmt.Println(b.A) }
./hello 1 2 3 4 {1 2}
调用匿名结构的方法
package main import "fmt" type innerS struct { in1 int in2 int } func (s * innerS) cmp() bool { return s.in1 > s.in2 } type outerS struct { b int c float32 int // anonymous field innerS //anonymous field } func main() { outer := new(outerS) outer.b = 6 outer.c = 7.5 outer.int = 60 outer.in1 = 5 outer.in2 = 10 fmt.Printf("outer.b is: %d ", outer.b) fmt.Printf("outer.c is: %f ", outer.c) fmt.Printf("outer.int is: %d ", outer.int) fmt.Printf("outer.in1 is: %d ", outer.in1) fmt.Printf("outer.in2 is: %d ", outer.in2) // 使用结构体字面量 outer2 := outerS{6, 7.5, 60, innerS{5, 10}} fmt.Println("outer2 is:", outer2) fmt.Println("outer2 is:", outer2.cmp()) }
./hello outer.b is: 6 outer.c is: 7.500000 outer.int is: 60 outer.in1 is: 5 outer.in2 is: 10 outer2 is: {6 7.5 60 {5 10}} outer2 is: false
但,golang同时也可以给结构体定义一个匿名interface field,用法:
标准库 sort
中,有下面的写法:
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
type reverse struct {
Interface
}
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
func Reverse(data Interface) Interface {
return &reverse{data}
}
reverse结构体内嵌了一个Interface
的interface,并且,提供了单独的Less
函数定义。
却没有提供 Len
, Swap
的定义。
首先,根据结构体内嵌其它匿名字段的定义,可以推知,理论上,调用reverse.Len
, reverse.Swap
,
肯定是会直接传递到 reverse.Interface.Len
和 reverse.Interface.Swap
,
即,和直接调用Interface的同名函数没有任何区别。
但,reverse
提供了单独的Less
函数,它的实现是颠倒了i,j
参数,仍然送入到Interface.Less
中去,
那么,得出的结果与直接使用Interface排序的结果,肯定是相反的。
为什么如此设计?
Meaning of a struct with embedded anonymous interface?
摘录其中比较关键的解释如下:
- In this way
reverse
implements thesort.Interface
and we can override a specific method without having to define all the others. - 结构体创建时,可以使用任何实现了
Interface
的obj来初始化,参考:
package main import "fmt" // some interface type Stringer interface { String() string } // a struct that implements Stringer interface type Struct1 struct { field1 string } func (s Struct1) String() string { return s.field1 } // another struct that implements Stringer interface, but has a different set of fields type Struct2 struct { field1 []string dummy bool } func (s Struct2) String() string { return fmt.Sprintf("%v, %v", s.field1, s.dummy) } // container that can embedd any struct which implements Stringer interface type StringerContainer struct { Stringer } func main() { // the following prints: This is Struct1 fmt.Println(StringerContainer{Struct1{"This is Struct1"}}) // the following prints: [This is Struct1], true fmt.Println(StringerContainer{Struct2{[]string{"This", "is", "Struct1"}, true}}) // the following does not compile: // cannot use "This is a type that does not implement Stringer" (type string) // as type Stringer in field value: // string does not implement Stringer (missing String method) //fmt.Println(StringerContainer{"This is a type that does not implement Stringer"}) }
./hello This is Struct1 [This is Struct1], true
package main import "fmt" // some interface type Stringer interface { String() string } // a struct that implements Stringer interface type Struct1 struct { field1 string } func (s Struct1) String() string { return s.field1 } // another struct that implements Stringer interface, but has a different set of fields type Struct2 struct { field1 []string dummy bool } func (s Struct2) String() string { return fmt.Sprintf("%v, %v", s.field1, s.dummy) } // container that can embedd any struct which implements Stringer interface type StringerContainer struct { Stringer } func main() { // the following prints: This is Struct1 fmt.Println(StringerContainer{Struct1{"This is Struct1"}}) // the following prints: [This is Struct1], true fmt.Println(StringerContainer{Struct2{[]string{"This", "is", "Struct1"}, true}}) // the following does not compile: // cannot use "This is a type that does not implement Stringer" (type string) // as type Stringer in field value: // string does not implement Stringer (missing String method) fmt.Pri
# command-line-arguments ./hello.go:46:35: cannot use "This is a type that does not implement Stringer" (type string) as type Stringer in field value: string does not implement Stringer (missing String method)
package main import ( "fmt" ) type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) } // Array 实现Interface接口 type Array []int func (arr Array) Len() int { return len(arr) } func (arr Array) Less(i, j int) bool { return arr[i] < arr[j] } func (arr Array) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] } // 匿名接口(anonymous interface) type reverse struct { Interface } // 重写(override) func (r reverse) Less(i, j int) bool { return r.Interface.Less(j, i) // j和i调换了 } // 构造reverse Interface func Reverse(data Interface) Interface { return &reverse{data} } func main() { arr := Array{1, 2, 3} rarr := Reverse(arr) fmt.Println(arr.Less(0,1)) fmt.Println(rarr.Less(0,1)) }
./hello true false
sort包中这么写的目的是为了重写Interface的Less方法,并有效利用了原始的Less方法;通过Reverse可以从Interface构造出一个反向的Interface。go语言利用组合的特性,寥寥几行代码就实现了重写。
对比一下传统的组合匿名结构体实现重写的写法,或许可以更好的帮助我们了解匿名接口的优点:
package main import ( "fmt" ) type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) } type Array []int func (arr Array) Len() int { return len(arr) } func (arr Array) Less(i, j int) bool { return arr[i] < arr[j] } func (arr Array) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] } // 匿名struct type reverse struct { Array } // 重写 func (r reverse) Less(i, j int) bool { return r.Array.Less(j, i) } // 构造reverse Interface func Reverse(data Array) Interface { return &reverse{data} } func main() { arr := Array{1, 2, 3} rarr := Reverse(arr) fmt.Println(arr.Less(0, 1)) fmt.Println(rarr.Less(0, 1)) }
./hello true false
上面这个例子使用了匿名结构体的写法,和之前匿名接口的写法实现了同样的重写功能,甚至非常相似。但是仔细对比一下你就会发现匿名接口的优点,匿名接口的方式不依赖具体实现,可以对任意实现了该接口的类型进行重写。这在写一些公共库时会非常有用,如果你经常看一些库的源码,匿名接口的写法应该会很眼熟。
匿名接口还有一个作用就是对结构体添加一些约束,必须使用实现了该接口的类型来构造实例。结构体中可以包含一些其他的字段,而interface只有方法,没有field。
package main import ( "fmt" "reflect" "sort" ) type Array1 []int func (arr Array1) Len() int { return len(arr) } func (arr Array1) Less(i, j int) bool { return arr[i] < arr[j] } func (arr Array1) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] } type Array2 []int func (arr Array2) Len() int { return len(arr) } func (arr Array2) Less(i, j int) bool { return arr[i] < arr[j] } func (arr Array2) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] } type Sortable struct { sort.Interface // other field Type string } func NewSortable(i sort.Interface) Sortable { t := reflect.TypeOf(i).String() return Sortable{ Interface: i, Type: t, } } func DoSomething(s Sortable) { fmt.Println(s.Type) fmt.Println(s.Len()) fmt.Println(s.Less(0, 1)) } func main() { arr1 := Array1{1, 2, 3} arr2 := Array2{3, 2, 1, 0} DoSomething(NewSortable(arr1)) DoSomething(NewSortable(arr2)) }
main.Array1 3 true main.Array2 4 false
反射匿名字段
root@ubuntu:~/go_learn/example.com/hello# cat hello.go //反射 是如何处理 匿名字段的? package main import ( "reflect" "fmt" ) type Stu struct { Id int Name string Age int } type Man struct { //这里你要注意一下,你创建的属性,是有顺序的,是有下标的 //如Stu 下标 就是0, title下标就是1 // Stu 就是匿名属性 Stu title string } func main() { //注意,对匿名字段进行初始化时的方式,其实本质上跟其他属性是一样的 m := Man{Stu:Stu{Id:2,Name:"Jack",Age:19}, title:"Manager"} t := reflect.TypeOf(m) //取匿名字段的方式 //FieldByIndex 方法,传入的是一个切片slice类型 //第1个0,表示,匿名字段在Man中的下标位置 //第2个0,表示,你要取匿名字段中哪个属性的下标 fmt.Printf("%#v ", t.FieldByIndex([]int{0,0})) //取的是id fmt.Printf("%#v ", t.FieldByIndex([]int{0,1})) //取的是Name fmt.Printf("%#v ", t.FieldByIndex([]int{0,2})) //取的是Age }
./hello reflect.StructField{Name:"Id", PkgPath:"", Type:(*reflect.rtype)(0xad5c0), Tag:"", Offset:0x0, Index:[]int{0}, Anonymous:false} reflect.StructField{Name:"Name", PkgPath:"", Type:(*reflect.rtype)(0xadd00), Tag:"", Offset:0x8, Index:[]int{1}, Anonymous:false} reflect.StructField{Name:"Age", PkgPath:"", Type:(*reflect.rtype)(0xad5c0), Tag:"", Offset:0x18, Index:[]int{2}, Anonymous:false}