• BlueKey@fedia.ioOP
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      Turned out that the bug ocurred randomly. The first tries I just had the “luck” that it only happened when the breakpoints were on.
      Fixed it by now btw.

        • BlueKey@fedia.ioOP
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          I’m new to Go and wanted to copy some text-data from a stream into the outputstream of the HTTP response. I was copying the data to and from a []byte with a single Read() and Write() call and expexted everything to be copied as the buffer is always the size of the while data. Turns out Read() sometimes fills the whole buffer and sometimes don’t.
          Now I’m using io.Copy().

          • dfyx@lemmy.helios42.de
            link
            fedilink
            arrow-up
            0
            ·
            1 year ago

            Note that this isn’t specific to Go. Reading from stream-like data, be it TCP connections, files or whatever always comes with the risk that not all data is present in the local buffer yet. The vast majority of read operations returns the number of bytes that could be read and you should call them in a loop. Same of write operations actually, if you’re writing to a stream-like object as the write buffers may be smaller than what you’re trying to write.