标签:col alc code import bsp sleep ack port cond
1.chan-数据交换
package main import ( "fmt" "time" ) func add(a int,b int,c chan int){ sum :=a+b time.Sleep(3*time.Second) c<-sum } func main(){ pipe:=make(chan int,1) go add(10,20,pipe) sum:= <-pipe fmt.Println("10+20=",sum) }
2.函数返回2个值
package main import ( "fmt" "time" ) func calc(a int, b int) (int, int) { sum := a + b avg := (a + b) / 2 return sum,avg } func add(a int, b int, c chan int) { sum := a + b time.Sleep(3 * time.Second) c <- sum } func main() { sum, avg := calc(10, 20) // sum, _ := calc(10, 20) fmt.Println("sum=", sum,"avg=",avg) }
标签:col alc code import bsp sleep ack port cond
原文地址:https://www.cnblogs.com/jec1999/p/9822466.html