Golang. The average of the numbers entered in the string

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 (

func main() {
	r := bufio.NewReader(os.Stdin)

	fmt.Printf("Enter some numbers: ")

	numsRaw, err := r.ReadString('\n')
	if err != nil {

	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 {
		x += tmp

	fmt.Println(x / len)


This entry was posted in Go (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.