NASutils Knowledge BaseUnofficialPolski

Go · lesson 6 of 30 · First stepsAsk the user

A bufio.Scanner reads lines, strconv.Atoi turns text into a number and says when it cannot.

Programming on the NAS › Go in 30 lessons

06-input.go Download 06-input.go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Print("Your name: ")
	in.Scan()
	name := in.Text()

	fmt.Print("Disks in the NAS: ")
	in.Scan()
	disks, err := strconv.Atoi(in.Text())
	if err != nil {
		fmt.Println("That is not a whole number")
		os.Exit(1)
	}
	fmt.Printf("Hello, %s. %d disks of 8 TB are %d TB.\n", name, disks, disks*8)
}

Terminal

$ ./06-input
Your name: Ann Example
Disks in the NAS: 4
Hello, Ann Example. 4 disks of 8 TB are 32 TB.
$ ./06-input
Your name: Ann Example
Disks in the NAS: four
That is not a whole number

Exercise

Ask again instead of ending the program.

TurboShell does not contain Go. These programs are made in Bucket & Code with its Go runtime; the finished file runs on the NAS without Go.