Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go scanf的行为出人意料?_Go - Fatal编程技术网

Go scanf的行为出人意料?

Go scanf的行为出人意料?,go,Go,下面是我的简单代码,每当我试图在终端上进入角色“破解”,有时它会很好地接受,有时它会奇怪地将其视为“德拉克”。它究竟为什么要这样做?这种情况经常发生 fmt.Println("\n Do you want crack a hash (type 'crack') or just help other nodes in cracking their hash (type 'help')?") fmt.Scanf("%s\n", &role) fmt.Println("role taken i

下面是我的简单代码,每当我试图在终端上进入角色“破解”,有时它会很好地接受,有时它会奇怪地将其视为“德拉克”。它究竟为什么要这样做?这种情况经常发生

fmt.Println("\n Do you want crack a hash (type 'crack') or just help other nodes in cracking their hash (type 'help')?")
fmt.Scanf("%s\n", &role)
fmt.Println("role taken is", role)
if role == "crack" {

这可能是因为您在
scanf
函数中使用了
\n
,而不是在
scanf
中使用
\n
,请尝试在后续的
Println
语句中使用它,如下所示:

fmt.Scanf("%s", &role)
fmt.Println("\nrole taken is", role)

那不会有什么区别吧?如何将c转换为D?是的,它应该解决这个问题,因为scanf函数扫描值,在这里,您传递了新行转义序列,这就是函数行为意外的原因。现在试着运行它,看看你是否发现了同样的行为,因为你展示了更多的代码<代码>角色声明等。您使用的是什么操作系统?什么终点站?您能用这种方式打印变量role吗?
fmt.Printf(“角色是%q”,role)
?(%q是显示引号)而且您不需要在Scanf调用中包含\n,对于您的示例,您可以使用
fmt.Scan(&role)