Question:
I have this piece of go code, mostly taken from here:Answer:
The callfmt.Scanf("%s", &roleName)
returns after reading the first whitespace character after the token.The line terminator on Windows is
\r\n
. The fmt.Scanf call returns after reading the \r
. The \n
remains in stdin. The later call to scanner.Scan()
reads the remaining \n
in stdin and returns immediately.The line terminator on other systems is
\n
. The call fmt.Scanf returns after reading the entire line terminator. The call to scanner.Scan()
waits for the user to type another line terminator.One fix is to use the scanner for all input:
If you have better answer, please add a comment about this, thank you!