golang, gouroutines, How to set up chanel in another chanel, and than read it after closing mother chanel

huangapple go评论78阅读模式
英文:

golang, gouroutines, How to set up chanel in another chanel, and than read it after closing mother chanel

问题

我在Golang方面还比较新,但我正在努力理解这门伟大的语言!请帮助我。

我有两个通道,一个是"In"通道,一个是"Out"通道。

in, out := make(chan Work), make(chan Work)

我设置了一些goroutine工作线程来监听in通道,获取工作并执行。我有一些工作,我想发送到In通道中。

当工作由工作线程完成后,它会将结果写入Out通道。

func worker(in <-chan Work, out chan<- Work, wg *sync.WaitGroup) {
    for w := range in {
        // 对工作进行处理
        time.Sleep(time.Duration(w.Z))
        out <- w
    }
    wg.Done()
}

当所有工作都完成后,我会在程序的适当时机关闭这两个通道。

现在,我想将已完成的工作结果写入OUT通道,但要将其分成几个部分,例如,如果工作类型是这样的:

type Work struct {
    Date    string
    WorkType string
    Filters  []Filter
}

如果WorkType是"firstType",我想将已完成的工作发送到一个通道,如果WorkType是"secondType",则发送到另一个通道... 但可能会有超过20种工作类型... 如何更好地解决这个问题?

我可以在OUT通道中设置通道的通道,并从这些子通道中获取数据吗?

附注:请原谅我这些初学者的问题。

英文:

I'm rather new in Golang, but working hard on understanding this great language! Please, help me in this..

I have 2 chanels. "In" and "Out" chanels

	in, out := make(chan Work), make(chan Work)

I set up goroutines workers that are listening for in chanel, grab work and do it. I have some work, that i would send into In chanel.

When Work is done by worker, it writes to the Out chanel.

func worker(in &lt;-chan Work, out chan&lt;- Work, wg *sync.WaitGroup) {
	for w := range in {

		// do some work with the Work

		time.Sleep(time.Duration(w.Z))
		out &lt;- w
	}
	wg.Done()
}

When all work is done I close both chanels at the write time of the program.

Now I want to write the results of done work in OUT chanel, but to separate all in some parts, for example, if work type would be like this :

type Work struct {
	Date string
	WorkType string
	Filters []Filter
}

if WorkType is "firstType" I would like to send the done work to one chanel, and if WorkType is "secondType" to second chan... But there might be over 20 types of work .. How to resolve this case in a better way?

Can I set up chanels in chanel OUT, and grab data from this sub chanels?

p.s.: Forgive me my noob questions, please..

答案1

得分: 1

你可以将输出通道设置为通用类型,并使用类型切换来处理不同的工作项。

假设你的输出通道只是 chan interface{}

准备好的工作项的消费者代码可能如下所示:

for item := range output {
   // 在每个 case 语句中,x 将具有适当的类型
   switch x := item.(type) {
       case workTypeOne:
          handleTypeOne(x)
       case workTypeTwo:
          handleTypeTwo(x)
       // 其他类型的处理...

       // 如果有人向通道发送了非工作项
       default: 
          panic("工作项的类型无效!")
   }
}

处理程序会处理特定类型的工作项,例如:

func handleTypeOne(w workTypeOne) { 
    // 处理工作项类型一的逻辑
    ....
}
英文:

You can have the output channel be generic, and handle different work items using a type switch.

Say your output channel is just chan interface{}.

The consumer of ready work items will look something like:

for item := range output {
   // in each case statement x will have the appropriate type
   switch x := item.(type) {
       case workTypeOne:
          handleTypeOne(x)
       case workTypeTwo:
          handleTypeTwo(x)
       // and so on...

       // and in case someone sent a non-work-item down the chan
       default: 
          panic(&quot;Invalid type for work item!&quot;)
   }
}

and the handlers handle a specific type, i.e.

func handleTypeOne(w workTypeOne) { 
    ....
}

huangapple
  • 本文由 发表于 2016年2月7日 22:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/35254979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定