Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I empty input stream? The fmt.Scanf in a main cannot use more than 2 times, the second will appear to skip a need for input. #13607

Closed
CodyGuo opened this issue Dec 14, 2015 · 3 comments

Comments

@CodyGuo
Copy link

CodyGuo commented Dec 14, 2015

How can I empty input stream?

package main

import (
"fmt"
)

func main() {
var a int
var f float32
var str string

fmt.Print("Please input : ")
fmt.Scanf("%d %f %s", &a, &f, &str)
fmt.Println("Output: ", a, f, str)

fmt.Println("----------------------------")

var sIP, sMac string
fmt.Print("Please input: ")
fmt.Scanf("%s %s", &sIP, &sMac)

fmt.Println("Output: ", sIP, sMac)

}

@mdempsky
Copy link
Contributor

@CodyGuo
Copy link
Author

CodyGuo commented Dec 14, 2015

I'm sorry, I don't have access to mailing lists and BBS address.

@ianlancetaylor
Copy link
Member

The mailing list and forums are accessible through a web browser, including posting new issues. I can't think of a reason why you would be able to access Github issues but not access them.

To empty the input stream you need to read from it until the end. Although your problem is not clear to me, It's likely that you should be using something like bufio.Readstring and fmt.Sscanf rather than using fmt.Scanf directly.

@golang golang locked and limited conversation to collaborators Dec 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants