General Stack Package Driver
package main import ( "fmt" "genstack" ) // Test and print emptiness result. func empty_test(s *genstack.Stack) { if s.Empty() { fmt.Println("Empty") } else { fmt.Println("Not Empty") } } func main() { var s genstack.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) }