Section6.54

This commit is contained in:
2023-10-04 14:55:12 +02:00
parent 39c8d853a5
commit 4e13ad8a10
3 changed files with 54 additions and 0 deletions

33
Section6/54.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
i, err := strconv.Atoi("45")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(i)
}
if i, err := strconv.Atoi("20X"); err == nil { //20X can not be converted to int
fmt.Println("No error, i is:", i)
} else {
fmt.Println(err)
}
if args := os.Args; len(args) != 2 {
fmt.Println("One argument is required")
} else if km, err := strconv.Atoi(args[1]); err != nil {
fmt.Println("The argument must be an integer! Error:", err)
} else {
fmt.Printf("%d km in miles is %v\n", km, float64(km)*0.621)
fmt.Printf("%T\n", args[1])
}
}