交换并发函数

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

Swap Concurrent Function

问题

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

package main

import "fmt"

func Swap(a, b int) (int, int) {
  return b, a
}

func main() {
  for i := 0; i < 10; i++ {
   fmt.Println(Swap(i+1, i))
  }
}

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

package main

import "fmt"

// 只有发送通道
func Swap(a, b chan<- int) {
	go func() {
		for i := 0; i < 10; i++ {
			a <- i + 1
			b <- i
		}
	}()
}

func main() {

	a := make(chan int)
	b := make(chan int)

	Swap(a, b)

	for i := 0; i < 10; i++ {
		fmt.Printf("chan a received: %d | chan b received: %d\n", <-a, <-b)
	}
}

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

英文:

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

package main 

import &quot;fmt&quot;

func Swap(a, b int) (int, int) {
  return b, a
}

func main() {
  for i := 0; i &lt;10; i++ {
   fmt.Println(Swap(i+1, i))
  }
}

So far I have came up with this solution:

package main

import &quot;fmt&quot;


// Only Send Channel
func Swap(a, b chan&lt;- int) {
	go func() {
		for i := 0; i &lt; 10; i++ {
			a &lt;- i + 1
			b &lt;- i
		}
	}()
}

func main() {

	a := make(chan int)
	b := make(chan int)

	Swap(a, b)

	for i := 0; i &lt; 10; i++ {
		fmt.Printf(&quot;chan a received: %d | chan b received: %d\n&quot;, &lt;-a, &lt;-b)
	}
}

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 工作面试中得到的问题,我当时无法回答,希望这对某人有所帮助。

// 将 int 切片类型的 channel 作为参数传递
// 通过 channel 发送交换后的信息
func Swap(a, b int, ch chan []int) {
	ch <- []int{b, a}
}

func main() {
	ch := make(chan []int)

	for i := 0; i < 100; i++ {
		// 使用创建的 channel 调用 Worker
		go Swap(i+1, i, ch)
	}

	for i := 0; i < 100; i++ {
		// 接收 channel 的值
		fmt.Println(<-ch)
	}
}

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

英文:

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.

// Pass as a parameter a int slice of type channel
// Send the swapped information throw the channel
func Swap(a, b int, ch chan []int) {
	ch &lt;- []int{b, a}
}

func main() {
	ch := make(chan []int)

	for i := 0; i &lt; 100; i++ {
		// Call Worker with the created channel
		go Swap(i+1, i, ch)
	}

	for i := 0; i &lt; 100; i++ {
		// Receive Channel Value
		fmt.Println(&lt;-ch)
	}
}

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:

确定