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())
}