空接口没有任何方法,所以所有的结构体都实现了空接口
空接口可以存储任何类型的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
type Queue []interface{}
func (q *Queue) (v interface{}) { *q = append(*q, v) }
func (q *Queue) Pop() interface{} { i := (*q)[0] *q = (*q)[1:] return i }
func (q *Queue) Empty() bool { return len(*q) == 0 }
q := interfaces.Queue{} q.Push(1) q.Push(true) q.Push("str") fmt.Println(q.Pop()) fmt.Println(q.Pop()) fmt.Println(q.Empty()) fmt.Println(q.Pop()) fmt.Println(q.Empty())
|
近期评论