FAQ
For both json and xml, encoding takes an io.Writer. However, the number of
bytes written to this writer is not exposed. It only returns the error
value. Unless I'm mistaken, this means that when using a *bufio.Writer, or
*os.File, I'll need to intercept this writing with my own writer that
captures the number of bytes written.

Does anyone else find this odd or frustrating? I think this should be
changed for Go 2.

--
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/groups/opt_out.

Search Discussions

  • Bill Thiede at Feb 21, 2014 at 3:44 am
    http://play.golang.org/p/5X9BD8jBQN

    You don't have to 'wrap', you can create a type that implements io.Writer
    and counts the bytes written to it. Then you use io.MultiWriter duplicate
    writes to both.

    If you were using an os.File, you can always f.Seek(0, os.SEEK_CUR) before
    and after to compare the number of bytes written.

    Bill

    On Thursday, February 20, 2014 5:31:04 PM UTC-8, ja...@jakesandlund.com
    wrote:
    For both json and xml, encoding takes an io.Writer. However, the number of
    bytes written to this writer is not exposed. It only returns the error
    value. Unless I'm mistaken, this means that when using a *bufio.Writer, or
    *os.File, I'll need to intercept this writing with my own writer that
    captures the number of bytes written.

    Does anyone else find this odd or frustrating? I think this should be
    changed for Go 2.
    --
    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/groups/opt_out.
  • Jake at Feb 21, 2014 at 3:59 am
    Thanks Bill. I think the MultiWriter approach is marginally better than
    "wrapping". I still think it's rather annoying that json/xml encoders have
    the bytes written information and just throw it
    away: http://golang.org/src/pkg/encoding/json/stream.go?s=3669:3716#L173
    On Thursday, February 20, 2014 9:44:15 PM UTC-6, Bill Thiede wrote:

    http://play.golang.org/p/5X9BD8jBQN

    You don't have to 'wrap', you can create a type that implements io.Writer
    and counts the bytes written to it. Then you use io.MultiWriter duplicate
    writes to both.

    If you were using an os.File, you can always f.Seek(0, os.SEEK_CUR) before
    and after to compare the number of bytes written.

    Bill

    On Thursday, February 20, 2014 5:31:04 PM UTC-8, ja...@jakesandlund.comwrote:
    For both json and xml, encoding takes an io.Writer. However, the number
    of bytes written to this writer is not exposed. It only returns the error
    value. Unless I'm mistaken, this means that when using a *bufio.Writer, or
    *os.File, I'll need to intercept this writing with my own writer that
    captures the number of bytes written.

    Does anyone else find this odd or frustrating? I think this should be
    changed for Go 2.
    --
    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/groups/opt_out.
  • Jesse McNelis at Feb 21, 2014 at 4:04 am

    On Fri, Feb 21, 2014 at 2:59 PM, wrote:

    Thanks Bill. I think the MultiWriter approach is marginally better than
    "wrapping". I still think it's rather annoying that json/xml encoders have
    the bytes written information and just throw it away:
    http://golang.org/src/pkg/encoding/json/stream.go?s=3669:3716#L173
    What use case do you have for it?

    --
    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/groups/opt_out.
  • Jake at Feb 21, 2014 at 4:38 am
    In my case, I wanted to know how many bytes I was writing to a json file.
    So yeah, I could do the seek trick, but that seems hacky, and I don't want
    to make a syscall.
    On Thursday, February 20, 2014 10:04:26 PM UTC-6, Jesse McNelis wrote:

    On Fri, Feb 21, 2014 at 2:59 PM, <ja...@jakesandlund.com <javascript:>>wrote:
    Thanks Bill. I think the MultiWriter approach is marginally better than
    "wrapping". I still think it's rather annoying that json/xml encoders have
    the bytes written information and just throw it away:
    http://golang.org/src/pkg/encoding/json/stream.go?s=3669:3716#L173
    What use case do you have for it?
    --
    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/groups/opt_out.
  • Jesse McNelis at Feb 21, 2014 at 5:18 am

    On Fri, Feb 21, 2014 at 3:38 PM, wrote:

    In my case, I wanted to know how many bytes I was writing to a json file.
    So yeah, I could do the seek trick, but that seems hacky, and I don't want
    to make a syscall.
    Wrapping a writer for counting is only 9 lines of code and can be reused
    for any io.Writer.
    http://play.golang.org/p/ckL_xrDSsC



    --
    =====================
    http://jessta.id.au

    --
    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/groups/opt_out.
  • Naitik Shah at Feb 21, 2014 at 5:25 am
    https://github.com/ParsePlatform/go.counting may be helpful. Though it
    is very little code like Jesse suggested.
    On Thu, Feb 20, 2014 at 9:18 PM, Jesse McNelis wrote:
    On Fri, Feb 21, 2014 at 3:38 PM, wrote:

    In my case, I wanted to know how many bytes I was writing to a json file.
    So yeah, I could do the seek trick, but that seems hacky, and I don't want
    to make a syscall.

    Wrapping a writer for counting is only 9 lines of code and can be reused for
    any io.Writer.
    http://play.golang.org/p/ckL_xrDSsC



    --
    =====================
    http://jessta.id.au

    --
    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/groups/opt_out.


    --
    -Naitik

    --
    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/groups/opt_out.
  • Mihai B at Mar 31, 2015 at 10:52 am
    I find it quite odd. I'm writing a storage abstraction package which
    basically just encodes the data before to write it. Part of this I need to
    implement the io.Writer interface. It seems that the encode method
    deliberately masks the number of bytes written so I would be interested to
    know why it doesn't return this data if it's already available.
    http://golang.org/src/encoding/json/stream.go?s=3433:3470#L136

      173 if _, err = enc.w.Write(e.Bytes()); err != nil { 174 enc.err = err 175 } 176 encodeStatePool.Put(e) 177 return err 178 }

    On Friday, 21 February 2014 01:31:04 UTC, ja...@jakesandlund.com wrote:

    For both json and xml, encoding takes an io.Writer. However, the number of
    bytes written to this writer is not exposed. It only returns the error
    value. Unless I'm mistaken, this means that when using a *bufio.Writer, or
    *os.File, I'll need to intercept this writing with my own writer that
    captures the number of bytes written.

    Does anyone else find this odd or frustrating? I think this should be
    changed for Go 2.
    --
    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
postedFeb 21, '14 at 2:45a
activeMar 31, '15 at 10:52a
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase