附上test代码,以便后续查看使用
package main
import (
“fmt”
“time”
)
type test1 struct {
a string
b int
c int
d int
}
func produce(p chan<- test1) {
aaa := test1{
a: “aaaaaa”,
b: 1,
c: 1,
}
for i := 0; i < 10; i++ {
aaa.b = i
aaa.c = i
aaa.d = i
p <- aaa
fmt.Println(“send:”, i)
time.Sleep(1 * time.Second)
}
}
func consumer(c <-chan test1) {
for i := 0; i < 10; i++ {
v := <-c
fmt.Println(“receive:”, v)
}
}
func main() {
ch := make(chan test1, 10)
go produce(ch)
go consumer(ch)
time.Sleep(20 * time.Second)
}