The source code that can be executed by calc in "Go Language programming" in Windows

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The first item in "Go Language Programming" is Calc, which, according to the author's description in the book, runs without problems under Linux. However, after practice, the program can not print out the results described in the book.

After testing, it was found that the Windows command line performs calc Add 2 3 o'clock, which also takes Calc as a parameter, so that the subscript of the parameter array in the program is different from the one described in the book.

Now upload the revised program for your reference.

Calc.go

Package Mainimport "OS" import "FMT" import "Simplemath" import "StrConv" var Usage = func () {fmt. Println ("Usage:calc command [arguments] ...") fmt. Println ("\nthe commands are:\n\tadd\taddition of the values.\n\tsqrt\tsquare root of a non-negative value.")} Func main () {args: = os. Argsfmt.println ("args:", args)//print arguments, find some difference with it in the book if args = = Nil | |  Len (args) < 2 {Usage () return}//arguments number is no sameswitch args[1] {case "add": If Len (args)! = 4 {//Arguments Number is not samefmt. Println ("Usage:calc add <integer1><integer2>") return}v1, err1: = StrConv. Atoi (args[2])//Arguments number is not samev2, err2: = StrConv. Atoi (args[3])//Arguments number is not sameif 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 {//Arguments number is not samefmt. 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 ()}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.