Golang : Fix fmt.Scanf() on Windows will scan input twice problem
If you ever need to write a program that use Golang's fmt.Scanf()
function on Windows operating system and test the program at the Command Prompt, chances are high that you will encounter weird output result such as a ghost that will hit the key on your behalf, adding 1+1=3 or unable to control your for-loop properly when processing the input parameter accepted by fmt.Scanf()
.
This is because fmt.Scanf()
function will behave differently on Windows. Simply put, for Windows, the "Enter" button is "\r\n", but in fmt
package, it only deal with "\n". See (https://github.com/golang/go/issues/5391)
To fix this issue, use
fmt.Scan()
(withoutf
) instead offmt.Scanf()
function or if you insist, add "\n" after %v such asfmt.Scanf(%v\n, &variable)
The program below demonstrate how to replicate the fmt.Scanf()
problem. You will have to run this in Windows Command Prompt.
package main
import (
"fmt"
)
func main() {
var number int
fmt.Print("Enter a number : ")
fmt.Scanf("%d", &number)
fmt.Println("Number is : ", number)
}
Run the program at Windows command prompt and the output will look like this:
C:\Users\Sweet Logic>scanf
Enter a number : 4
Number is : 4
Enter a string : String is :
Notice that instead of waiting for you to enter the string input...it will just go ahead and accept the empty input. This ONLY happens on Windows Command Prompt. It will NOT happen on Linux, Unix or even Gogland Editor running on Windows.
GOROOT=C:/Go
GOPATH=C:/work
C:/Go\bin\go.exe build -i -o "C:\Users\SWEETL~1\AppData\Local\Temp\Build main.go and rungo" C:/work/src/setReadOnly/main.go
"C:\Program Files\JetBrains\Gogland 171.3780.106\bin\runnerw.exe" "C:\Users\SWEETL~1\AppData\Local\Temp\Build main.go and rungo"
Enter a number : 12
Number is : 12
Enter a string : abasdasd
String is : abasdasd
Process finished with exit code 0
See also : Golang : Scanf function weird error in Windows
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.7k Golang : Get current, epoch time and display by year, month and day
+12.5k Golang : Pass database connection to function called from another package and HTTP Handler
+5.3k Swift : Convert string array to array example
+22k Golang : Convert seconds to minutes and remainder seconds
+13.8k Golang : convert rune to unicode hexadecimal value and back to rune character
+16.7k Golang : Get own process identifier
+5.4k PHP : Fix Call to undefined function curl_init() error
+9.8k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+4.8k Python : Find out the variable type and determine the type with simple test
+9.9k CodeIgniter : Load different view for mobile devices
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+8k How to show different content from website server when AdBlock is detected?