Hi Ian >> Check the error result from Fscanln. Thank you for the advice but I still have some arguments about wrong behavior of Fscanln() or not fully correct documentation for it..
Let’s look at the next lines from the documentation. *func **Fscan* <https://cs.opensource.google/go/go/+/go1.23.5:src/fmt/scan.go;l=121> func Fscan(r io <https://pkg.go.dev/io>.Reader <https://pkg.go.dev/io#Reader>, a ...any <https://pkg.go.dev/builtin#any>) (n int <https://pkg.go.dev/builtin#int>, err error <https://pkg.go.dev/builtin#error>) Fscan scans text read from r, storing successive *space-separated values into successive arguments*. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. *func **Fscanln <https://cs.opensource.google/go/go/+/go1.23.5:src/fmt/scan.go;l=130>* func Fscanln(r io <https://pkg.go.dev/io>.Reader <https://pkg.go.dev/io#Reader>, a ...any <https://pkg.go.dev/builtin#any>) (n int <https://pkg.go.dev/builtin#int>, err error <https://pkg.go.dev/builtin#error>) Fscanln *is similar to **Fscan <https://pkg.go.dev/fmt@go1.23.5#Fscan>*, but* stops scanning at a newline *and after the final item there must be a newline or EOF. --- So it means that Fscanln() scans text read from r, storing successive *space-separated values into successive arguments*, but *stops scanning at a newline*. I prepared the new code, please look at it. https://go.dev/play/p/xDGYbwdzMpL In the table below you can see the result of the code. In the row 1 from the table we see that Fscanln() scans till the space and reads the *right value *"Go" and returns also an error "expected newline". The error message in this case means that the reading was successful but the newline wasn't achieved - "expected newline". For all other values in this line the behavior of Fscanln() differs (row 2-4). The function reads the wrong values (without 1st char) with the same error message except the row 4 in the table where the newline was found and the error is nil.. For the second line of the source string we see the same results (row 5 with right value, rows 6-11 with shrinked values). The Fscanln() *behaves differently *for the first and other values in a line, and this is the error because documentation tells: "Fscanln *is similar to * Fscan". That's the problem. The error "expected newline" resulting from Fscan() is only a message not an error similar to io.EOF. We can interpret "expected newline" as nothing dangerous just continue to read till the newline. So concerning the documentation after successful reading the first value in a line we should continue the scanning. [image: FscanlnErr.png] `ivan` On Monday, January 27, 2025 at 7:36:39 AM UTC+3 Ian Lance Taylor wrote: On Sun, Jan 26, 2025 at 4:51 PM 'Ivan Burak' via golang-nuts <golan...@googlegroups.com> wrote: > > Fscan() eats 1st char in scanning values if the values aren't a first value in a line. > > The documentation tells: "Fscanln is similar to Fscan, but stops scanning at a newline and after the final item there must be a newline or EOF." > > > > The test discovers a bug in Fscanln() - https://go.dev/play/p/Jem2lWVn3H5 Check the error result from Fscanln. Ian -- 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. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/875875d9-ff68-4f7b-bb7f-133a815d9d34n%40googlegroups.com.