如果在GO中通过通道发送大型 struct ,那么它实际上是在goroutines之间复制的吗?
例如,在下面的代码中,Go会在goroutines生产者和消费者之间实际复制所有largeStruct数据吗?
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++
}
}
}