Section6.54
This commit is contained in:
21
Section6/52.go
Normal file
21
Section6/52.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* fmt.Println("os.Args", os.Args)
|
||||||
|
fmt.Println("Path:", os.Args[0])
|
||||||
|
fmt.Println("1st argument:", os.Args[1])
|
||||||
|
fmt.Println("2nd argument:", os.Args[2])
|
||||||
|
fmt.Println("No if items inside os.Args:", len(os.Args)) */
|
||||||
|
|
||||||
|
var results, err = strconv.ParseFloat(os.Args[1], 64)
|
||||||
|
fmt.Println(results)
|
||||||
|
fmt.Printf("%T\n", os.Args[1])
|
||||||
|
fmt.Printf("%T\n", results)
|
||||||
|
_ = err
|
||||||
|
}
|
||||||
33
Section6/54.go
Normal file
33
Section6/54.go
Normal 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])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user