Let’s write a program that reads N numbers separated by a space (in one line) and calculates their average value.
The task seems to be simple, but it can be solved in different ways. Here is my version:
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { r := bufio.NewReader(os.Stdin) fmt.Printf("Enter some numbers: ") numsRaw, err := r.ReadString('\n') if err != nil { panic(err) } nums := strings.TrimSpace(numsRaw) numSplit := strings.Split(nums, " ") len := len(numSplit) var x int for i := range numSplit { tmp, err := strconv.Atoi(numSplit[i]) if err != nil { panic(err) } x += tmp } fmt.Println(x / len) }