码迷,mamicode.com
首页 > 编程语言 > 详细

go 语言学习十 - 通道

时间:2018-09-03 02:16:48      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:语言   fun   ons   str   学习   port   art   question   class   

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 语言学习十 - 通道

标签:语言   fun   ons   str   学习   port   art   question   class   

原文地址:https://www.cnblogs.com/scala/p/9576369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!