1 package main 2 3 import "fmt" 4 5 func sum(values [] int , resultChan chan int){ 6 sum:=0 7 for _, value := range values{ 8 sum += value 9 } 10 resultChan <- sum 11 } 12 13 func main(){ 14 values := [] int{1,2,3,4,5,6,7,8,9,10} 15 resultChan := make(chan int, 2) 16 go sum(values[:len(values)/2],resultChan) 17 go sum(values[len(values)/2:],resultChan) 18 sum1, sum2 := <-resultChan, <-resultChan 19 20 fmt.Println("Result:",sum1,sum2,sum1+sum2) 21 }
1 {} 格式要求
2 package main , func main
3 默认支持gdb
4 函数可返回多个值
有疑问加站长微信联系(非本文作者)