Nice. Thanks!

            b0 := scanner.Bytes()

            b1 := make([]byte, len(b0))

            copy(b1, b0)

            outChannel <- b1


Chris

On Mon, Aug 29, 2016 at 8:41 PM, Caleb Spare <cesp...@gmail.com> wrote:

> You can do
>
> b0 := scanner.Bytes()
> b1 := make([]byte, len(b0))
> copy(b0, b1)
> outChannel <- b0
>
> On Mon, Aug 29, 2016 at 8:23 PM, chris.lu via golang-nuts
> <golang-nuts@googlegroups.com> wrote:
> > I am reading a file line by line, and send it to a chan []byte, and
> consume
> > it on another goroutine.
> >
> > However, I found I must create a new []byte, because the scanner.Bytes()
> > returned a []byte slice that's shared, and the scanner may still write to
> > the []byte slice.
> >
> > How to efficiently create a new []byte slice that's not shared?
> >
> > The simple way I can think of is to []byte(string(bytes)).
> > Or am I approach this correctly at all?
> >
> > Chris
> >
> >         scanner := bufio.NewScanner(file)
> >
> >         for scanner.Scan() {
> >
> >             // this conversion to string and then to []byte is needed.
> >
> >             // calling scanner.Bytes() will cause malformed lines.
> >
> >             outChannel <- []byte(scanner.Text())
> >
> >         }
> >
> >
> > --
> > 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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to