package main
import "fmt"
// Test and print emptiness result.
func empty_test(s *Stack) {
if s.empty() {
fmt.Println("Empty")
} else {
fmt.Println("Not Empty")
}
}
func main() {
var s Stack
empty_test(&s)
// Put some stuff.
for i := 1; i < 10; i++ {
s.push(i)
}
empty_test(&s)
// Lose some.
for i := 1; i < 5; i++ {
// Pop the item. The data is the integer, but its declared
// type is the interface.
data, _ := s.pop()
// The odd notation is a "type assertion" It converts the
// type or panics if the data is not of that type (integer
// in this case).
fmt.Printf("%d ", data.(int))
}
fmt.Println()
empty_test(&s)
// Now, let's put some other things on.
strings := [6]string{"there","are","some","strings","here","now"}
d := 3.8
for i := 0; i < len(strings); i++ {
s.push(strings[i])
d = 3.487*d
s.push(d)
}
empty_test(&s)
// Dump it all
for {
data, ok := s.pop()
if !ok { break }
// This is a type switch. As it sounds, it switches on the
// type of a value, not a value. The variable v has the
// matched type inside of each case.
switch v := data.(type) {
case int:
fmt.Println("Your integer, sir:", v)
case string:
fmt.Println("String value here:", v)
case float64:
fmt.Println("Float away:", v)
default:
fmt.Println("Don't know what kind of thingie")
}
}
empty_test(&s)
}