sort: use fmt.Scan in ExampleSearch_guessingGame to handle leftover n… - #80734
sort: use fmt.Scan in ExampleSearch_guessingGame to handle leftover n…#80734AkariH wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
d072a01 to
43b129f
Compare
|
This PR (HEAD: 43b129f) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/810760. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/810760. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/810760. |
|
Message from Cocoa: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/810760. |
|
Message from Cocoa: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/810760. |
What does this PR do?
This PR fixes an interactive input glitch in
ExampleSearch_guessingGamewithin thesort.Searchdocumentation example insrc/sort/search.go.Root Cause & Problem
Currently,
ExampleSearch_guessingGameusesfmt.Scanf("%s", &s)inside the binary search closure passed tosort.Search:When executed in a real interactive TTY session:
'y'followed byEnterleaves the newline character'\n'inos.Stdin's buffer.sort.Search,fmt.Scanf("%s", &s)reads the leftover'\n', fails to parse a non-empty string token, and returns immediately without waiting for keyboard input.Solution
Replace
fmt.Scanf("%s", &s)withfmt.Scan(&s).fmt.Scanautomatically consumes and discards leading whitespace and newline characters ('\n'), ensuring that each iteration correctly blocks and waits for user input.