On Monday, 29 August 2016 23:30:48 UTC-4, chri...@uber.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? > If you're concerned about efficiency, don't send a copy of every token over a channel; consume it in the same goroutine. A typical scanner is a stateful function (or a method of an object) that returns a pair (token int, text []bytes), where the int describes the kind of token you are looking at. You only need to look at the text for literal strings and numbers, and you usually don't need to retain a copy of it.
Rob's talk about scanning with channels is a lot of fun, but I wouldn't use that approach in production. -- 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.