Yakog
Yakog

Reputation: 129

Why does "cat" sometimes skip bytes when reading from another terminal, but the golang scan function does not?

One question related to pty-s (linux ubuntu Terminals; pseudo-terminals) and the way (cat) golang reads from them.

First, I did the following.

  1. open two terminals (dev/pts/0 - 0 and dev/pts/1 - 1)
  2. then I type in second terminal "cat dev/pts/0" (so whatever is written on the first terminal will be displayed on the second)
  3. start writing in the first terminal (TUI)

It generally works in most of the time, but some characters are skipped from time to time.

More precisely, for those characters that are not displayed in the second terminal, I have echo, while for those that are displayed, I do not. It's as if echo and cat are competing for characters.

For example, if I write "Hello world, what are you doing", source terminal window (/dev/pts/0) displays: "el od eyn". On the other side, destination terminal windows (/dev/pts/1) displays: "Hlowrl,what ar ou doig"

However, when I work with golang and fmt scan functions, whenever I write something to terminal (and thus to /dev/pts/0) scan function reads everything from it (no missing bytes) + entered content is displayed in the TUI ("echo" works in this case).

package main

import "fmt"

func main() {
    var input string

    fmt.Println("Enter text:")

    for {
        fmt.Scanln(&input)
        fmt.Println("You wrote:", input)
    }
}

Thus, in this case, It looks like "competition" between echo and golang doesn't exist...

I expect the same behavior in both cases. In particular, to read all bytes in both cases and to have (or not have) an echo in both cases.

What is the difference? Why does "cat" sometimes skip bytes when reading from another terminal, but the golang scan function does not? I thought the golang just reads from the "dev/pts/0" in the same way as "cat" does and just gives the result.

Upvotes: 2

Views: 145

Answers (0)

Related Questions