package main import "fmt" /* * The defer function. */ func demo() { // Keep count and total of entered numbers. tot := 0 count := 0 // Cleanup to give the final stats. finisher := func() { fmt.Printf("=== You entered %d numbers, average %g ===\n", count, float32(tot)/float32(count)) } defer finisher() // Words for input numbers. words := [20]string { "zip", "wumpus", "goobers", "thump", "ding!", "teeth", "snorkel", "airplane", "warble", "snack", "northern", "morph", "angle", "tongs", "conduit", "island", "sunrise", "orbit", "dice", "meatball" } // Read numbers and print them, with their words. for { // Read a number. var val int fmt.Print("Pls enter a number: ") rdct, _ := fmt.Scan(&val) if rdct == 0 { break } // Print the number and its word. fmt.Printf("You entered %d: %s\n", val, words[val]) // Count and total. count++ tot += val } } func main() { // This function runs essentially after main finishes. The // systax here both creates a function and calls it, never giving // it a name. defer func() { fmt.Println("And now we are past done.") } () demo() fmt.Println("Now the main program is done") }