"Go language Programming" "1.5 Project Management" CALC.GO code error

Source: Internet
Author: User
Tags square root

recently saw by the people post and Telecommunications publishing house Xu Xiwei Lu Guihua, " go language programming", "the 1th chapter of the" Go Language "" 1.5 Project Management " , found the sample code calc.go several errors, args: = OS. The args array variable args[0] represents the program itself, and the 3 if statement len (args) condition determines that the right value is also 1 smaller, and that the anonymous function pointed to by the usage () will be executed when the code is written in the book, as shown below :

Usage:calc command [arguments] ...

The commands are:
Add addition of the values.
sqrt Square root of a non-negative value.

The correct calc.go code is as follows:

//Calc.go
PackageMain

Import(
"FMT"
"OS"
"Simplemath"
"StrConv"
)

varUsage =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. ")
}

funcMain() {
args: = os. Args
ifargs = =Nil||Len(args) <3{
Usage()
return
}

Switchargs[1] {
Case"Add":
ifLen(args)! =4{
Fmt.Println("Usage:calc add <integer1><integer2>")
return
}
V1, err1: = StrConv.Atoi(args[2])
V2, ERR2: = StrConv.Atoi(args[3])
ifErr1! =Nil|| Err2! =Nil{
Fmt.Println("Usage:calc add <integer1><integer2>")
return
}
RET: = Simplemath.ADD(v1, v2)
Fmt.Println("Result:", ret)
Case"sqrt":
ifLen(args)! =3{
Fmt.Println("Usage:cale sqrt <integer>")
return
}
V, err: = StrConv.Atoi(args[2])
ifErr! =Nil{
Fmt.Println("Usage:calc sqrt <integer>")
return
}
RET: = Simplemath.Sqrt(v)
Fmt.Println("Result:", ret)
default:
Usage()
}
}

This article is from the "Pine" blog, be sure to keep this source http://dongsong.blog.51cto.com/916653/1587144

"Go language Programming" "1.5 Project Management" CALC.GO code error

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.