// parse.gopackageparseimport ("fmt""strings""strconv")// A ParseError indicates an error in converting a word into an integer.typeParseErrorstruct { Index int// The index into the space-separated list of words. Word string// The word that generated the parse error. Err error// The raw error that precipitated this error, if any.}// String returns a human-readable error message.func (e *ParseError) String() string {return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word)}// Parse parses the space-separated words in in put as integers.funcParse(input string) (numbers []int, err error) {deferfunc() {if r :=recover(); r !=nil {var ok bool err, ok = r.(error)if!ok { err = fmt.Errorf("pkg: %v", r) } } }() fields := strings.Fields(input) numbers =fields2numbers(fields)return}funcfields2numbers(fields []string) (numbers []int) {iflen(fields) ==0 {panic("no words to parse") }for idx, field :=range fields { num, err := strconv.Atoi(field)if err !=nil {panic(&ParseError{idx, field, err}) } numbers =append(numbers, num) }return}