dty97501 2018-10-09 10:29
浏览 55
已采纳

如何在函数之间传递带有chan的var

I'm implementing a simple mechanism of passing variable between two goroutines with a channel. Here is my code:

pipe := make(chan string)
go func(out chan string, data string) { //1st goroutine
        out <- DataSignerMd5(data)
    }(pipe, data)
go func(in chan string) {               //2nd goroutine
        data := <-in
        in <- DataSignerCrc32(data)
    }(pipe)
crcMdData := <- pipe

More likely, crcMdData pulls a variable from pipe before 2nd goroutine. I guess that I simply can create another channel to make this work. But maybe it's possible with a single pipe?

  • 写回答

1条回答 默认 最新

  • douhan8009 2018-10-09 10:55
    关注

    You should use a second channel for what you want to do. You could get away with using a single channel and switching on the result, but that's not really ideal - you're basically trying to put two different types of objects into the same channel, and your program will end up being a lot cleaner and easier to reason about if you just have one channel per data type / intended transformation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题