交换并发函数

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

Swap Concurrent Function

问题

我正在尝试弄清楚如何使Go语言中的前向Swap函数并发,以便学习概念目的:

  1. package main
  2. import "fmt"
  3. func Swap(a, b int) (int, int) {
  4. return b, a
  5. }
  6. func main() {
  7. for i := 0; i < 10; i++ {
  8. fmt.Println(Swap(i+1, i))
  9. }
  10. }

到目前为止,我想出了这个解决方案:

  1. package main
  2. import "fmt"
  3. // 只有发送通道
  4. func Swap(a, b chan<- int) {
  5. go func() {
  6. for i := 0; i < 10; i++ {
  7. a <- i + 1
  8. b <- i
  9. }
  10. }()
  11. }
  12. func main() {
  13. a := make(chan int)
  14. b := make(chan int)
  15. Swap(a, b)
  16. for i := 0; i < 10; i++ {
  17. fmt.Printf("chan a received: %d | chan b received: %d\n", <-a, <-b)
  18. }
  19. }

它似乎可以工作,但我不能确定。
我该如何衡量这个,并且有人知道如何真正使一个简单的Swap函数在Go中并发吗?

英文:

I am trying to figure out how to make concurrent the forward Swap function in Go for learning concepts purpose:

  1. package main
  2. import &quot;fmt&quot;
  3. func Swap(a, b int) (int, int) {
  4. return b, a
  5. }
  6. func main() {
  7. for i := 0; i &lt;10; i++ {
  8. fmt.Println(Swap(i+1, i))
  9. }
  10. }

So far I have came up with this solution:

  1. package main
  2. import &quot;fmt&quot;
  3. // Only Send Channel
  4. func Swap(a, b chan&lt;- int) {
  5. go func() {
  6. for i := 0; i &lt; 10; i++ {
  7. a &lt;- i + 1
  8. b &lt;- i
  9. }
  10. }()
  11. }
  12. func main() {
  13. a := make(chan int)
  14. b := make(chan int)
  15. Swap(a, b)
  16. for i := 0; i &lt; 10; i++ {
  17. fmt.Printf(&quot;chan a received: %d | chan b received: %d\n&quot;, &lt;-a, &lt;-b)
  18. }
  19. }

It seems to work but I cannot be sure about it.
How can I measure this and does anyone knows how to really make a simple Swap function concurrent in Go?

答案1

得分: 0

很抱歉,我知道如何解决这个问题。花了我几天的时间才能做到这一点,就在我在这个论坛上发布问题几个小时后,我发现了解决方法。我将发布代码和解决方法,这是我在一次高级 Golang 工作面试中得到的问题,我当时无法回答,希望这对某人有所帮助。

  1. // 将 int 切片类型的 channel 作为参数传递
  2. // 通过 channel 发送交换后的信息
  3. func Swap(a, b int, ch chan []int) {
  4. ch <- []int{b, a}
  5. }
  6. func main() {
  7. ch := make(chan []int)
  8. for i := 0; i < 100; i++ {
  9. // 使用创建的 channel 调用 Worker
  10. go Swap(i+1, i, ch)
  11. }
  12. for i := 0; i < 100; i++ {
  13. // 接收 channel 的值
  14. fmt.Println(<-ch)
  15. }
  16. }

我非常感谢任何关于这个问题的评论、改进和概念参考。

英文:

I am sorry for the question but I figured out how to solve this.
It took days for me to be able to do this and on the day I posted at this forum some hours later I discovered how to solve it.
I am going to post the code and the walkthrough this was given to me in a Senior Golang Job Interview and I could not answer, I hope this can help someone.

  1. // Pass as a parameter a int slice of type channel
  2. // Send the swapped information throw the channel
  3. func Swap(a, b int, ch chan []int) {
  4. ch &lt;- []int{b, a}
  5. }
  6. func main() {
  7. ch := make(chan []int)
  8. for i := 0; i &lt; 100; i++ {
  9. // Call Worker with the created channel
  10. go Swap(i+1, i, ch)
  11. }
  12. for i := 0; i &lt; 100; i++ {
  13. // Receive Channel Value
  14. fmt.Println(&lt;-ch)
  15. }
  16. }

I really appreciate any comments, improvements and conceptual references on this.

huangapple
  • 本文由 发表于 2021年10月29日 00:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/69757378.html
匿名

发表评论

匿名网友

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

确定