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

bufio: bug in the emptyFinalToken example for Scanner #25909

pam4 opened this issue Jun 15, 2018 · 2 comments


None yet
4 participants
Copy link

commented Jun 15, 2018

The SplitFunc in the emptyFinalToken example for bufio.Scanner doesn't handle the case of requesting more data if necessary.

The example only happens to work because the whole input is returned by a single call to Read (it uses a strings.Reader).

This should be enough to fix it:

@@ -21,7 +21,10 @@
        // There is one final token to be delivered, which may be the empty string.
        // Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
        // but does not trigger an error to be returned from Scan itself.
-       return 0, data, bufio.ErrFinalToken
+       if atEOF {
+           return 0, data, bufio.ErrFinalToken
+       }
+       return 0, nil, nil
    // Scan.

The problem is demonstrated here:


This comment has been minimized.

Copy link

commented Jun 16, 2018

I think we should include logic like the OP includes since that's an important general pattern for the Scanner and none of the other examples directly include logic for requesting more data - all the other examples (transitively) have similar checks but only via their usage of the Scan* SplitFuncs.

cc: @ianlancetaylor

@andybons andybons added this to the Unplanned milestone Jun 18, 2018


This comment has been minimized.

Copy link

commented Jan 14, 2019

Change mentions this issue: bufio: fix emptyFinalToken example to handle multiple Reads

@gopherbot gopherbot closed this in 5126fea Mar 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.