Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:53:17+00:00 2026-06-11T03:53:17+00:00

I would like to read from the original stdin of a Go program. For

  • 0

I would like to read from the original stdin of a Go program. For example, if I did echo test stdin | go run test.go, I would want to have access to “test stdin”. I’ve tried reading from os.Stdin, but if there’s nothing in it, then it will wait for input. I also tried checking the size first, but the os.Stdin.Stat().Size() is 0 even when input is passed in.

What can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:53:19+00:00Added an answer on June 11, 2026 at 3:53 am

    I think your question per se has no sensible answer because there’s just no such thing as "initial stdin". Unix-like OSs, and Windows implement the concept of "standard streams", which works like this (simplified): when a process is created, it automagically has three file descriptors (handles in Windows) open — stdin, stdout and stderr. No doubts, you’re familiar with this concept, but I’d like to stress the meaning of the word "stream" there — in your example, when you call

    $ echo 'test stdin' | ./stdin
    

    the shell creates a pipe, spawns two processes (one for echo and one for your binary) and makes use of the pipe it created: the pipe’s write FD is attached to the echo‘s stdout and the pipe’s read FD is attached to your binary’s stdin. Then whatever the echo process pleases to write to its stdout is piped (sic!) to the stdin of your process.
    (In reality most today’s shells implement echo as a built-in primitive but this does not in any way change the semantics; your could as well have tried /bin/echo instead, which is a real program. Also note that I just used ./stdin to refer to your program — this is for clarity, as go run stdin.go would do exactly this, in the end.)

    Note several crucial things here:

    • The writing process (echo in your case) is not oblidged to write anything to its stdout (for instance, echo -n would not write anything to its stdout and exit successfully).
    • It’s also able to make arbitrary delays writing its data (either because it wants to make such delays or because it has been preempted by the OS or sleeps in some syscall waiting on some busy system resource etc).
    • The OS buffers transfers over pipes, and often the code in the program does this, too—though it may not be apparent for an inexperienced programmer. This means what the writing process sends to a pipe, might come out in arbitrary chunks on the reading side.1
    • There are only two ways to know the writing side has no more data to send over the pipe:
      • Somehow encode this in the data itself (this means using an agreed upon data transfer protocol between the writer and the reader).
      • The writer might close its side of the pipe which would result in the "end of file" condition on the reader side (but only after the buffer is drained and one another call to read is attempted, which fails).

    Let’s wrap this up: the behaviour you’re observing is correct and normal. If you expect to get any data from stdin, you must not expect it to be readily available. If you also don’t want to block on stdin, then create a goroutine which would do blocking reads from stdin in an endless loop (but checking for the EOF condition) and pass collected data up over a channel (possibly after certain processing, if needed).

    1 This is why certain tools which usually occur between two pipes in a pipeline, such as grep, might have special options to make them flush their stdout after writing each line — read about the --line-buffered option in the grep manual page for one example. People who are not aware of this "full buffering by default" semantics are puzzled why tail -f /path/to/some/file.log | grep whatever | sed ... seems to stall and not display anything when it’s obvious the monitored file gets updated.


    As a side note: if you were to run your binary "as is", like in

    $ ./stdin
    

    that would not meant the spawned process would not have stdin (or "initial stdin" or whaveter), instead, its stdin would be connected to the same stream your shell receives your keyboard input from (so you could directly type something to your process’s stdin).

    The only sure way to have a process’s stdin connected to nowhere is to use

    $ ./stdin </dev/null
    

    on Unix-like OSes and

    C:\> stdin <NUL
    

    on Windows. This "null device" makes the process see EOF on the first read from its stdin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read asynchronously from stdin with Qt. I don't want to
I would like to know how to read elements from a const boost::multi_array object.
I would like to write a function which will read values from a text
I would like to know if PHP is Object Oriented, I read that from
I would like to read a DICOM file in C#. I don't want to
I would like to retrieve the original file name from a website while downloading
I would like to know how I can read a number from a text
I would like my program to be able to access a website that processes
I would like to read multiple XML files in an array and then parse
I would like to read Rails documentation locally though gem server. I go to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.