我正在尝试一个应该测试 fmt.Scanf 的片段,但它似乎没有按预期工作:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("What is your favorite color?")
var favoriteColor string
fmt.Scanf("%s", &favoriteColor)
fmt.Println("Fave color is", favoriteColor)
fmt.Println("What is your favorite food?")
var myfood string
fmt.Scanf("%s", &myfood)
fmt.Printf("I like %s too!\n", myfood)
fmt.Printf("Wait two seconds please...\n")
time.Sleep(2000 * time.Millisecond)
fmt.Printf("Your favorite color is %s, and the food you like best is %q\n", favoriteColor, myfood)
}
然而只接受第一个答案,程序继续到最后然后返回:
What is your favorite color?
red
Fave color is red
What is your favorite food?
I like too!
Wait two seconds please...
Your favorite color is red, and the food you like best is ""
为什么第二个 scanf 函数被忽略?这对我来说毫无意义。
我在 Windows 7 上使用最新的 64 位软件包安装了 Go。
慕尼黑5688855
相关分类