我试图从用户那里获取输入并将其打印出来,但显然距离的值是0,在我做了一些输入之后。
这是代码:
type Person struct {
name string
age int
ambition string
}
func (p *Person) walking() {
var distance int
fmt.Println("Enter the distance")
fmt.Scanf("%f", &distance)
if distance < 9 {
fmt.Println(p.name, " is walking towards that direction in ", distance,
"kilometer")
} else {
fmt.Println("This is an error, ignore this")
}
}
正如您所看到的,每当我尝试在主函数上运行此命令时,fmt.Println(p.name, " is walking towards that direction in ", distance, "kilometer")即使距离的值大于9.
func main() {
p := new(Person)
p.name = "Joker"
p.walking()
}
p.walking()将始终打印距离为0?谁能向我解释这里发生了什么?
四季花海
相关分类