【问题标题】:how comparing value from channel in Go如何在 Go 中比较来自通道的值
【发布时间】:2020-12-17 04:13:12
【问题描述】:

我有两个通道,首先给我一些字符串,我需要过滤到相同的值,然后结果需要发送到第二个通道

func main() {
    c := make(chan string, 5)
    o := make(chan string, 5)
    arr := []string{"aa", "ab", "ab", "bb", "bb", "ba", "cc"}
    for _, v := range arr {
        c <- v
        go removeDuplicates(c, o)
        time.Sleep(1 * time.Second)
        fmt.Println("output: ", <-o)
    }
}

func removeDuplicates(cIn, cOut chan string) {
   last := ""
   for cur, isOpen := <-cIn; isOpen; {
      if cur != last {
        fmt.Printf("val: %s, last: %s\n", cur, last) 
        last = cur
        cOut <- cur
        //close(cOut)
      }
   }
}

我尝试将以前的值保存到“last”变量,但是当我运行程序时,“last”为空

val: aa, last: 
output:  aa
val: ab, last: 
output:  ab
val: ab, last:

我也不知道在这种情况下何时需要关闭哪些频道。 感谢您的帮助和关注

【问题讨论】:

  • 您的代码无法编译。请提供minimal reproducible example
  • 如果你能添加一个带有游乐场链接的小例子会很好
  • last = &lt;- cur 也许?
  • 对不起各位,这是一个测试任务,我自己没有看到所有代码,我试图在操场上重现它,但它通常会报错:“超时运行程序”play.golang.org/p/MpUyPkkP0XF
  • @SaeedEntezari 否 cur 不是 chan string 而是 string。因此你不能做&lt;-cur

标签: go concurrency channel goroutine


【解决方案1】:

首次修复removeDuplicates()

问题是您的for statement 中有一个空的post 声明:

for cur, isOpen := <-cIn; isOpen; {
    // ..
}

所以你从cIn 频道收到了一次,但你永远不会收到更多,你在 post 语句中什么都不做,所以你只是重复循环体,没完没了。

一旦循环体被执行,你必须再次接收:

for cur, isOpen := <-cIn; isOpen; cur, isOpen = <-cIn {
    // ..
}

有了这个,输出将是(在Go Playground上试试):

val: aa, last: 
output:  aa
val: ab, last: aa
output:  ab
val: ab, last: 
output:  ab
val: bb, last: ab
output:  bb
val: bb, last: 
output:  bb
val: ba, last: ab
output:  ba
val: cc, last: 
output:  cc

但最好在频道上使用for range

for cur := range cIn {
    if cur != last {
        fmt.Printf("val: %s, last: %s\n", cur, last)
        last = cur
        cOut <- cur
    }
}

这输出相同。在Go Playground 上试试这个。

现在开始修复main()

我们看到“无效”输出,输出中的值仍然重复。

这是因为您启动了多个运行 removeDuplicates() 的 goroutine。这很糟糕,因为在输入通道上发送的值将被多个 goroutine 接收,如果重复的值没有被一个接收到,它们仍然可以被检测为唯一的,因此相同的值将多次发送到输出.

让一个生产者在输入通道上发送所有值,一旦所有值都发送完毕,关闭通道。

有一个使用for range过滤值的goroutine,一旦循环退出(所有输入值都被消耗),关闭输出通道。

并且有一个 goroutine 从输出通道接收值,使用 for range,这样你就可以消除那个丑陋的 time.Sleep

func main() {
    c := make(chan string, 5)
    o := make(chan string, 5)

    go func() {
        arr := []string{"aa", "ab", "ab", "bb", "bb", "ba", "cc"}
        for _, v := range arr {
            c <- v
        }
        close(c)
    }()

    go removeDuplicates(c, o)

    for v := range o {
        fmt.Println("output: ", v)
    }
}

func removeDuplicates(cIn chan string, cOut chan string) {
    last := ""
    for cur := range cIn {
        if cur != last {
            fmt.Printf("val: %s, last: %s\n", cur, last)
            last = cur
            cOut <- cur
        }
    }
    close(cOut)
}

这将输出(在Go Playground 上尝试):

val: aa, last: 
val: ab, last: aa
val: bb, last: ab
val: ba, last: bb
val: cc, last: ba
output:  aa
output:  ab
output:  bb
output:  ba
output:  cc

【讨论】:

    【解决方案2】:

    我已对代码进行了注释,以便您理解。请参考以下代码:

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
        var (
            c  = make(chan string, 1)
            o  = make(chan string, 1)
            wg = sync.WaitGroup{}
        )
    
        stream := []string{"aa", "ab", "ab", "bb", "bb", "ba", "cc"}
    
        wg.Add(1)
        // Wait until all the values are received
        defer wg.Wait()
        // Getter receives the filtered out stream
        go getter(o, &wg)
    
        // Removes duplicates from the stream
        go removeDuplicates(c, o)
    
        // Send elems to removeDuplicates
        for _, elem := range stream {
            c <- elem
        }
        // Close the channel
        close(c)
    }
    
    // getter recieves the filtered out elements
    func getter(cOut <-chan string, wg *sync.WaitGroup) {
        defer wg.Done()
        for val := range cOut {
            fmt.Println("Output: ", val)
        }
    }
    
    // removeDuplicates removes the adjacent duplicates
    func removeDuplicates(cIn chan string, cOut chan string) {
        var last string
        for cur := range cIn {
            if cur != last {
                fmt.Printf("val: %s, last: %s\n", cur, last)
                last = cur
                cOut <- cur
            }
        }
        close(cOut)
    }
    

    【讨论】:

    • 也谢谢你@shmsr,这是测试任务,我只能更改 removeDuplicates 函数,但我真的很感谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多