表达式
代码如下:
package main
import (
t "tools"
)
func main() {
t.Printfln("%08b", 1)
t.Printfln("%08b", 2)
t.Printfln("1&2=%08b%", 1&2)
t.Printfln("1&2=%d", 1&2)
}
运行结果 如下:
main.go:4:2: package tools is not in GOROOT (C:\Program Files\Go\src\tools)
后来再次运行结果:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
00000001
00000010
1&2=00000000%!
(MISSING)1&2=0
[Done] exited with code=0 in 10.727 seconds
体重指数的代码如下:
package main
import (
t "tools"
)
func main() {
W := 65.0
H := 1.75
BMI := W / (H * H)
t.Printfln("BMI: %.2f", BMI)
if BMI < 18.5 {
t.Printfln("偏瘦")
} else if (18 <= BMI) && (BMI < 24) {
t.Printfln("正常")
} else if 24 <= BMI && BMI < 28 {
t.Printfln("偏胖")
} else if 28 <= BMI && BMI < 30 {
t.Printfln("肥胖")
} else if BMI >= 30 {
t.Printfln("重度肥胖")
}
}
运行结果如下:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
BMI: 21.22
正常
[Done] exited with code=0 in 9.032 seconds
代码如下:
package main
import (
"fmt"
"os"
t "tools"
)
func main() {
args := os.Args
var W float64
var H float64
fmt.Sscanf(args[1], "%f", &W)
fmt.Sscanf(args[2], "%f", &H)
t.Printfln("体重:%.2f", W)
t.Printfln("身高:%.2f", H)
BMI := W / (H * H)
t.Printfln("BMI: %.2f", BMI)
if BMI < 18.5 {
t.Printfln("偏瘦")
} else if (18 <= BMI) && (BMI < 24) {
t.Printfln("正常")
} else if 24 <= BMI && BMI < 28 {
t.Printfln("偏胖")
} else if 28 <= BMI && BMI < 30 {
t.Printfln("肥胖")
} else if BMI >= 30 {
t.Printfln("重度肥胖")
}
}
运行结果如下:
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go run main.go 60 1.70
体重:60.00
身高:1.70
BMI: 20.76
正常
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go>
另外一种方法的运行结果:(不出结果)
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go install main.go
go install: no install location for .go files listed on command line (GOBIN not set)
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go build main.go
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> main 60 1.70
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> main.exe 60 1.70
main.exe : The term 'main.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. C
heck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ main.exe 60 1.70
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (main.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command main.exe was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\main.exe". See "get-help about_Command_Precedence" for more details.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> .\main 60 1.70
体重:60.00
身高:1.70
BMI: 20.76
正常
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go>
代码如下:
package main
import (
"os"
t "tools"
)
func main() {
args := os.Args
for i, v := range args {
t.Printfln("命令行参数%v: %v", i, v)
}
}
运行结果如下:
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go build main.go
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> main abc 123 "How are you!"
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> main.exe abc 123 "How are you!"
main.exe : The term 'main.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was i
ncluded, verify that the path is correct and try again.
At line:1 char:1
+ main.exe abc 123 "How are you!"
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (main.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command main.exe was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\main.exe". See "get-help about_Command_Precedence" for more details.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> .\main.exe abc 123 "How are you!"
命令行参数0: C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.exe
命令行参数1: abc
命令行参数2: 123
命令行参数3: How are you!
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go>
Go语言实践:从基础到实战
本文介绍了Go语言的基础表达式、代码实例,如BMI计算与参数解析,还探讨了包导入问题及不同运行方式下的结果。通过实际操作演示了如何处理输入参数、计算体重指数,并涉及了错误处理和命令行参数处理。
286

被折叠的 条评论
为什么被折叠?



