构建通用类型或包含不同类型变量的数组源代码
// 构建通用类型或包含不同类型变量的数组
package main
import "fmt"
type Element interface{}
type Vector struct {
a []Element
}
func (p *Vector) Get(i int) Element {
return p.a[i]
}
func (p *Vector) Set(i int, e Element) {
p.a[i] = e
}
func main() {
struct1 := &Vector{make([]Element, 10)}
i := 3
struct1.Set(i, "String")
fmt.Printf("index%d: %T\n", i, struct1.Get(i))
i = 1
struct1.Set(i, 120)
fmt.Printf("index%d: %T\n", i, struct1.Get(i))
i = 9
struct1.Set(i, false)
fmt.Printf("index%d: %T\n", i, struct1.Get(i))
fmt.Println(struct1)
}