func main() { args := os.Args[1:] opt := args[0] switch opt { case "add": r1, err1 := strconv.Atoi(args[1]) r2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("add strconv error", r1, r2) } res := simplemath.Add(r1, r2) fmt.Println(res) /* case "sqrt" : r1, err1 := strconv.Atoi(arg[1]) if(err1 != nil){ fmt.Println("sqrt strconv err", err1) } res := simplemath.sqrt(r1) fmt.Println(res) */ default: Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } fmt.Println(len(args)) fmt.Println(args[0]) fmt.Println(args[1]) fmt.Println(args[2]) switch args[1] { case "add": if len(args) != 4 { fmt.Println("USAGE: calc add <int1> <int2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <int1> <int2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) default: fmt.Println("Unknown command") } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usge() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("USAGE1:calc add <integer1><integer2>") } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE2:calc add <integer1><integer2>", err1) return } ret := simplemath.Add(v1, v2) fmt.Println("Result1:", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE3:calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE4:calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result2:", ret) default: Usge() } }
func main() { he := &oop.Person{"hezuoxiang", 10} he.Say("f**k") var a oop.Integer = 1 var b oop.Number = &a fmt.Println("Result: ", b.Less(2)) fmt.Println("Result: ", simplemath.Add(3, 4)) }
func main() { var args = os.Args if nil == args || len(args) <= 2 { fmt.Println("Arguments to less!") Usage() os.Exit(0) } switch args[1] { case "add": if 4 != len(args) { fmt.Println("USAGE: calc add integer integer") Usage() os.Exit(0) } a, err1 := strconv.Atoi(args[2]) b, err2 := strconv.Atoi(args[3]) if nil != err1 || nil != err2 { fmt.Println("Can not convert string to integer!") Usage() os.Exit(0) } ret := simplemath.Add(a, b) fmt.Println(a, "+", b, " = ", ret) case "sqrt": if 3 != len(args) { fmt.Println("USAGE: calc sqrt integer") Usage() os.Exit(0) } a, err := strconv.Atoi(args[2]) if nil != err { fmt.Println("Can not convert string to integer!") Usage() os.Exit(0) } ret, err := simplemath.Sqrt(a) if nil != err { fmt.Println("Can not use ", a, " in sqrt function!") Usage() os.Exit(0) } fmt.Println("Sqrt(", a, ") = ", ret) default: Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { showAddUsage() return } //strconv.Atoi: change str into integer v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { showAddUsage() return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { showSqrtUsage() return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() } }
func main() { args := os.Args[1:] if args == nil || len(args) < 2 { Usage() return } //fmt.Println("args 0 is:", args[0], args[1], args[2]) //fmt.Println("length is:", len(args)) /*for i,v := range args { fmt.Println(i," ", v) } */ switch args[0] { case "add": if len(args) != 3 { fmt.Println("USAGE1: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("USAGE2: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() fmt.Println("why me") } }
func main() { args := os.Args if args == nil || len(args) < 3 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("Usage: cale add <integer1> <integer2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("Usage: cale add <integer1> <integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 3 { fmt.Println("Usage: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("Usage: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: fmt.Println("unknown command ", args[1]) Usage() } }
func main() { args := os.Args switch args[0] { case "add": if len(args) != 3 { fmt.Println("USAGE: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": fmt.Println("sqrt") } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } fmt.Println("%s\n", args) switch args[1] { case "add": if len(args) != 4 { ErrAdd() return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { ErrAdd() return } ret := simplemath.Add(v1, v2) fmt.Println("result: %d ", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: %d ", ret) default: Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } switch args[1] { case "add": arg1, err1 := strconv.Atoi(args[2]) arg2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("err") return } ret := simplemath.Add(arg1, arg2) fmt.Println("Return ", ret) case "sqrt": default: Usage() } }
func main() { for i, v := range os.Args { fmt.Printf("%d:\t%s\n", i, v) } args := os.Args[1:] if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { fmt.Println("Usage calc add <integer><interger2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("Usage calc add <integer><interger2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("Usage calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("Usage calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("ResultL: ", ret) default: Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("USAGE: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() //fmt.Printf("len(args): %d, args[0] : %s , args[1]: %s, args[2]: %d, args[3]: %d",len(args), args[0], args[1], args[2], args[3]) } }
func main() { args := os.Args // fmt.Println(len(args)) if args == nil || len(args) < 2 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("Usage:add 3 8") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE:add 3 4") return } ret := simplemath.Add(v1, v2) fmt.Println("Result:", ret) case "sqrt": if len(args) != 3 { fmt.Println("sqrt 3-->") } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("这样输入:sqrt 3.") return } ret := simplemath.Sqrt(v) fmt.Println(ret) default: fmt.Println("default") } }
func main() { flag.Parse() args := flag.Args() if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { fmt.Println("USAGE: calc add <integer1> <integer2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <integer1> <integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() } }
func main() { args := os.Args if len(args) < 2 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("Usage: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("Usage: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) default: Usage() } }
func main() { ret := simplemath.Add(1, 2) fmt.Println("Result: ", ret) }
func main() { fmt.Println(simplemath.Add(1, 2)) fmt.Println("Hello world") }