Creating a array of extended types
I have a struct A, extending ("subclassing") it with struct B, like this:
package main
type A struct {
x int
}
type B struct {
A
y int
}
I want to create a array where I can append A or B to it, so that this code works:
func main() {
var m [2]B
m[0] = B { A { 1 }, 2 }
m[0].x = 0
m[0].y = 0
m[1] = A { 3 }
m[1].x = 0
}
It doesn't. If I create the array of the type B, I get "cannot use struct literal (type A) as type B in assignment". If I try to create the array of the type A, I get the the same error (just with the types reversed).
So my question is: which type should the a开发者_StackOverflow中文版rray be?
You could use struct
values. For example,
package main
import "fmt"
type A struct {
x int
}
type B struct {
A
y int
}
func main() {
var m []interface{}
m = append(m, B{A{1}, 2})
m = append(m, A{3})
fmt.Println(m[0], m[1])
if b, ok := m[0].(B); ok {
b.x = 0
b.y = 0
m[0] = b
}
if a, ok := m[1].(A); ok {
a.x = 0
m[1] = a
}
fmt.Println(m[0], m[1])
}
Output:
{{1} 2} {3}
{{0} 0} {0}
Or, you could use struct
pointers. For example,
package main
import "fmt"
type A struct {
x int
}
type B struct {
A
y int
}
func main() {
var m []interface{}
m = append(m, &B{A{1}, 2})
m = append(m, &A{3})
fmt.Println(m[0], m[1])
if b, ok := m[0].(*B); ok {
b.x = 0
b.y = 0
}
if a, ok := m[1].(*A); ok {
a.x = 0
}
fmt.Println(m[0], m[1])
}
Output:
&{{1} 2} &{3}
&{{0} 0} &{0}
You'll want to define the array type to interface{}
rather than B
. Then you can store both types in there. That's the only way to accomplish this. If both types implement a specific interface, then you can type to that instead of the generic interface{}
精彩评论