for !intContainer.IsEmpty() { elem, ok := intContainer.Get().(int) if !ok { fmt.Println("Get() did not return an integer.") continue } fmt.Printf("Elem: %d (%[1]T)\n", elem) }
}
虽然用了 type assertion,但是一旦你放进去了一个异类对象,立即 error 退出了:
1 2 3 4
intContainer := &Container{} intContainer.Put(1) intContainer.Put("5") //Get() did not return an integer. intContainer.Put(3)
for !intContainer.IsEmpty() { elem, ok := intContainer.Get().(int) if !ok { fmt.Println("Get() did not return an integer.") continue } fmt.Printf("Elem: %d (%[1]T)\n", elem) }