go 语言学习十 - 通道

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 //角度
}

猜你喜欢

转载自www.cnblogs.com/scala/p/9576369.html