go 語言學習十 - 通道
阿新 • • 發佈:2018-09-03
oat true out dia light n) pac func coo
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 語言學習十 - 通道