FAQ
I am a little confused with the buffering mode of Stdout, and fmt.Print
behaviour.

If I am not mistaken, it seems that os.Stdout is not line buffered.
In fact, it is not buffered at all, as shown here :

   http://play.golang.org/p/8IrFdcETpt

Is the buffering mode of Stdin, Stdout and Stderr documented somewhere ? I
haven't found so far ...

When the output of a Go program is redirected to a file, is there some
block buffering taking place ?

If I want to create a "line buffered" Writer wrapping os.Stdout, f :=
bufio.NewWriter(os.Stdout) is not enough.
I should create a custom Writer type, with a Write(s) method calling
Flush() if s contains '\n', correct ?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Aug 12, 2015 at 5:27 pm

    On Wed, Aug 12, 2015 at 10:21 AM, nicolas riesch wrote:

    I am a little confused with the buffering mode of Stdout, and fmt.Print
    behaviour.

    If I am not mistaken, it seems that os.Stdout is not line buffered.
    In fact, it is not buffered at all, as shown here :

    http://play.golang.org/p/8IrFdcETpt
    That is correct. os.Stdout is not buffered.

    Is the buffering mode of Stdin, Stdout and Stderr documented somewhere ? I
    haven't found so far ...
    It's never really seemed necessary to explicitly document that they
    are unbuffered. They are documented as being *os.File. A *os.File is
    not buffered.

    When the output of a Go program is redirected to a file, is there some block
    buffering taking place ?
    Not by the Go program. The operating system presumably does it.

    If I want to create a "line buffered" Writer wrapping os.Stdout, f :=
    bufio.NewWriter(os.Stdout) is not enough.
    I should create a custom Writer type, with a Write(s) method calling Flush()
    if s contains '\n', correct ?
    Correct.

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nicolas riesch at Aug 12, 2015 at 5:46 pm
    Aha moment:

    In C, printf, fprintf functions write to a FILE *stream, and "stdout" is a
    "stream", not a file.

    In Go, fmt.Print, fmt.Fprintf functions write directly to a file, not a
    Writer (equivalent to a C "stream").

    It is just that the "stdout" word means different things in C and Go ...

    Thank you for the explanation ;-)))

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Aug 12, 2015 at 5:51 pm
    io.Writer is not equivalent to a C "stream", whatever you meant by that.
    The only way you get (write) buffering in Go is if you are working with a
    bufio.Writer. C's standard library tends to buffer by default and you have
    to turn it off or use syscalls directly to avoid it. Go's standard library
    tends not to buffer by default and you need to wrap in bufio.* to get it.
    And don't forget to Flush() or Close() your files if they are buffered!
    On Wed, Aug 12, 2015 at 12:47 PM nicolas riesch wrote:

    Aha moment:

    In C, printf, fprintf functions write to a FILE *stream, and "stdout" is a
    "stream", not a file.

    In Go, fmt.Print, fmt.Fprintf functions write directly to a file, not a
    Writer (equivalent to a C "stream").

    It is just that the "stdout" word means different things in C and Go ...

    Thank you for the explanation ;-)))

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nicolas riesch at Aug 12, 2015 at 5:59 pm
    Please ignore my last message.

    In Go, fmt.Print, fmt.Fprintf functions also write to a Writer. But a File
    is just an unbuffered Writer.

    I must be a little tired ...

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 12, '15 at 5:21p
activeAug 12, '15 at 5:59p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase