Go · lesson 7 of 30 · Decisions and loops · bonusDecide with if
if, else if and else choose a block. A short statement may stand before the condition.
07-if.go Download 07-if.go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
fmt.Print("Disk temperature in C: ")
line, _ := bufio.NewReader(os.Stdin).ReadString('\n')
temp, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil {
fmt.Println("Type a whole number")
return
}
if temp < 0 || temp > 100 {
fmt.Println("Type a number from 0 to 100")
} else if temp >= 55 {
fmt.Println("Too hot: check the fans")
} else if temp >= 45 {
fmt.Println("Warm: look again in an hour")
} else {
fmt.Println("Fine")
}
}
Terminal
$ ./07-if
Disk temperature in C: 38
Fine
$ ./07-if
Disk temperature in C: 58
Too hot: check the fans
$ ./07-if
Disk temperature in C: hot
Type a whole numberExercise
Ask for the free space in percent and answer fine, low or full.
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.