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
<[email protected]> 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 [email protected].
> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to