Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to write a distinct channel in Go?

Tags:

go

I am a beginner in go.

I am trying to figure out an easy way to implement a channel that only output distinct values.

What I want to do is this:

package example

import (
    "fmt"
    "testing"
)

func TestShouldReturnDistinctValues(t *testing.T) {

    var c := make([]chan int)

    c <- 1
    c <- 1
    c <- 2
    c <- 2
    c <- 3

    for e := range c {
        // only print 1, 2 and 3.
        fmt.println(e)      
    }
}

Should I be concern about memory leak here if I were to use a map to remember previous values?

like image 681
Misterhex Avatar asked Aug 13 '14 14:08

Misterhex


2 Answers

You really can't do that, you'd have to keep a track of the values somehow, a map[int]struct{} is probably the most memory efficient way.

A simple example:

func UniqueGen(min, max int) <-chan int {
    m := make(map[int]struct{}, max-min)
    ch := make(chan int)
    go func() {
        for i := 0; i < 1000; i++ {
            v := min + rand.Intn(max)
            if _, ok := m[v]; !ok {
                ch <- v
                m[v] = struct{}{}
            }
        }
        close(ch)
    }()

    return ch
}
like image 118
OneOfOne Avatar answered Oct 20 '22 04:10

OneOfOne


I have done similar things before, except my problem was output inputs in ascending order. You can do this by adding a middle go routine. Here is an example:

package main

func main() {
    input, output := distinct()

    go func() {
        input <- 1
        input <- 1
        input <- 2
        input <- 2
        input <- 3
        close(input)
    }()

    for i := range output {
        println(i)
    }
}

func distinct() (input chan int, output chan int) {
    input = make(chan int)
    output = make(chan int)

    go func() {
        set := make(map[int]struct{})
        for i := range input {
            if _, ok := set[i]; !ok {
                set[i] = struct{}{}
                output <- i
            }
        }
        close(output)
    }()
    return
}
like image 24
chendesheng Avatar answered Oct 20 '22 05:10

chendesheng