构建通用类型或包含不同类型变量的数组源代码

// 构建通用类型或包含不同类型变量的数组

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)
}
编程就像呼吸,学会那天起一日不敢荒废。
ZsmHub
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!