Go 中如何比较来自通道的值

how comparing value from channel in Go

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

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:

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

第一次修复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()

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

这是因为您启动了多个 goroutines 运行 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

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

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)
}