On Tue, Sep 4, 2018 at 5:28 PM Hunter Breathat <breathat.hun...@gmail.com>
wrote:


> I only had stated that bufio.Scan() doesn't return io.EOF as the doc
stats that it Err returns nil. Which is fine but hard to check for. But
from what I've seen the functionality that im looking for would need to
check for the SIG.KILL/EOF on input. Now whether or not i shoudlgo and
alter the bufio.Scanner to return io.EOF as an Err is a different thing.
But that would be a smidge excessive for just 1 minor functionality.

.Scan() returns a bool value. io.EOF is not a bool, neither is's a rune ^D.
Typical usage of scanner is to loop in `for s.Scan() { do something }`.
After the loop one checks s.Err(). If it's nil, then scanner input is
exhausted (the underlying readre returned io.EOF). If s.Err() is not nil,
something else happened.

The reason that no ^D is ever seen to signal io.EOF while reading from
stdin - or any file for that matter - is that it would be an in-band
signal, so no files containing that character could be easily processed.

Example:

==== jnml@r550:~/tmp> cat main.go
package main

import (
"bufio"
"fmt"
"os"
)

func main() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
fmt.Printf("s.Text() returns %q\n", s.Text())
fmt.Printf("s.Err() returns %v\n", s.Err())
}
fmt.Printf("final s.Text() returns %q\n", s.Text())
fmt.Printf("final s.Err() returns %v\n", s.Err())
}
==== jnml@r550:~/tmp> go build && ./tmp
after this line we press <enter> and ^D
s.Text() returns "after this line we press <enter> and ^D"
s.Err() returns <nil>
final s.Text() returns ""
final s.Err() returns <nil>
==== jnml@r550:~/tmp>

-- 

-j

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to