import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("myflags", flag.ExitOnError) var myFloat float64 fs.Float64Var(&myFloat, "myfloat", 0.0, "a float64 flag") fs.Parse(os.Args[1:]) fmt.Println("myFloat value:", myFloat) }
import ( "flag" "fmt" "math" ) func main() { fs := flag.NewFlagSet("calc", flag.ExitOnError) var x, y float64 fs.Float64Var(&x, "x", 0.0, "the first number") fs.Float64Var(&y, "y", 0.0, "the second number") fs.Parse(os.Args[1:]) sum := x + y diff := x - y prod := x * y quot := math.NaN() if y != 0 { quot = x / y } fmt.Printf("Sum: %f\nDifference: %f\nProduct: %f\nQuotient: %f\n", sum, diff, prod, quot) }This example creates a new FlagSet called "calc" and defines two float64 flags called "x" and "y". When the code is run with the command-line arguments `./calculator -x 5 -y 3`, it will calculate and print out the sum, difference, product, and quotient of x and y. If y is 0, the quotient will be NaN (not a number).