If a large structure is sent down the channel in Go, is it really being copied between goroutines?
For example, in the code below, will Go actually copy all largeStruct data between the goroutines producer and the consumer?
package main import ( "fmt" "sync" ) type largeStruct struct { buf [10000]int } func main() { ch := make(chan largeStruct) wg := &sync.WaitGroup{} wg.Add(2) go consumer(wg, ch) go producer(wg, ch) wg.Wait() } func producer(wg *sync.WaitGroup, output chan<- largeStruct) { defer wg.Done() for i := 0; i < 5; i++ { fmt.Printf("producer: %d\n", i) output <- largeStruct{} } close(output) } func consumer(wg *sync.WaitGroup, input <-chan largeStruct) { defer wg.Done() i := 0 LOOP: for { select { case _, ok := <-input: if !ok { break LOOP } fmt.Printf("consumer: %d\n", i) i++ } } }
Playground: http://play.golang.org/p/fawEQnSDwB
source share