FAQ
Hi,
In the "Effective Go" document is a function:
func ReadFull(r Reader, buf []byte) (n int, err error) {
      for len(buf) > 0 && err == nil {
          var nr int
          nr, err = r.Read(buf)
          n += nr
          !buf = buf[nr:]
      }
      return
}
I think the marked line could be
buf = buf[n:]
If I am wrong the please explain me why.
Thanks

--
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

  • Igor Mihalik at Nov 14, 2013 at 11:36 am
    To me it looks correct as it is. "nr" contains number of bytes read by
    r.Read(buf), so for the next "Read" you want to continue and store data in
    buf[nr:]. And because buf is reused in each iteration, only increment "nr"
    is needed instead of absolute total "n".

    To compare for illustration, this would be the version of code using "n"

    func ReadFull(r Reader, buf []byte) (n int, err error) {
    b := buf
    for len(buf) > 0 && err == nil {
    var nr int
    nr, err = r.Read(buf)
    n += nr
    buf = b[n:]
    }
    return
    }
    On Thursday, November 14, 2013 9:12:23 AM UTC, Lubos Pintes wrote:

    Hi,
    In the "Effective Go" document is a function:
    func ReadFull(r Reader, buf []byte) (n int, err error) {
    for len(buf) > 0 && err == nil {
    var nr int
    nr, err = r.Read(buf)
    n += nr
    !buf = buf[nr:]
    }
    return
    }
    I think the marked line could be
    buf = buf[n:]
    If I am wrong the please explain me why.
    Thanks
    --
    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.
  • Chris Hines at Nov 14, 2013 at 2:33 pm

    On Thursday, November 14, 2013 4:12:23 AM UTC-5, Lubos Pintes wrote:
    Hi,
    In the "Effective Go" document is a function:
    func ReadFull(r Reader, buf []byte) (n int, err error) {
    for len(buf) > 0 && err == nil {
    var nr int
    nr, err = r.Read(buf)
    n += nr
    !buf = buf[nr:]
    }
    return
    }
    I think the marked line could be
    buf = buf[n:]
    If I am wrong the please explain me why.
    Thanks
    Each time buf is resliced on the marked line buf[0] of the new slice is the
    same as buf[nr] of the old slice. Thus on the second iteration buf[nr:] is
    the same as backingArray[nr1+nr2:] where nr1 = nr from the first iteration
    and nr2 = nr from the second iteration.

    --
    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.
  • Lubos Pintes at Nov 15, 2013 at 3:31 pm
    Thank for explanation.

    Dňa 14. 11. 2013 15:33 Chris Hines wrote / napísal(a):
    On Thursday, November 14, 2013 4:12:23 AM UTC-5, Lubos Pintes wrote:

    Hi,
    In the "Effective Go" document is a function:
    func ReadFull(r Reader, buf []byte) (n int, err error) {
    for len(buf) > 0 && err == nil {
    var nr int
    nr, err = r.Read(buf)
    n += nr
    !buf = buf[nr:]
    }
    return
    }
    I think the marked line could be
    buf = buf[n:]
    If I am wrong the please explain me why.
    Thanks


    Each time buf is resliced on the marked line buf[0] of the new slice is
    the same as buf[nr] of the old slice. Thus on the second iteration
    buf[nr:] is the same as backingArray[nr1+nr2:] where nr1 = nr from the
    first iteration and nr2 = nr from the second iteration.

    --
    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.

    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 14, '13 at 9:11a
activeNov 15, '13 at 3:31p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase