.. highlight:: go numstats1.go ============ :: // numstats1.go // To run at the command line: // $ go run numstats1.go package main import ( "bufio" "fmt" "os" "strconv" ) func main() { // numbers.txt is assumed to have one integer per line file, err := os.Open("numbers.txt") // deferred statements are run after the function ends defer file.Close() // if err is not nil, then something went wrong opening the file // and so we immediately end the program by calling panic if err != nil { panic("couldn't open numbers.txt") } // read all the numbers into a slice nums := []int{} // nums is initially empty scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() n, err := strconv.Atoi(line) if err == nil { nums = append(nums, n) } else { // err != nil fmt.Printf("Couldn't convert \"%v\" to an int (skipping)\n", line) } } // for fmt.Println(nums) } // main