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.