package main import ( "fmt" "math" ) /** c <- answers(chan) <- (cartesian struct) goroutine (polarCoord struct) <- questions(chan) <- polar{5, 30.5} */ func main() { questions := make(chan polar) defer close(questions) answers := make(chan cartesian) defer close(answers) go func() { for { p := <-questions radian := p.angle * math.Pi / 180.0 //弧度 x := p.radius * math.Cos(radian) y := p.radius * math.Sin(radian) answers <- cartesian{x, y} } }() questions <- polar{5, 30.5} c := <-answers fmt.Printf("(%.3f,%.3f)", c.x, c.y) } func process (questions chan polar, answers chan cartesian){ } type cartesian struct { x float64 y float64 } type polar struct { radius float64 angle float64 //角度 }
go 语言学习十 - 通道
猜你喜欢
转载自www.cnblogs.com/scala/p/9576369.html
今日推荐
周排行