This is a creation in Article, where the information may have evolved or changed.
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