Using pointer to channel

Gujarat Santana picture Gujarat Santana · Jun 4, 2017 · Viewed 9.2k times · Source

Is it good practice to use pointer to channel? For example I read the data concurrently and pass those data map[string]sting using channel and process this channel inside getSameValues().

func getSameValues(results *chan map[string]string) []string {
    var datas = make([]map[string]string, len(*results))
    i := 0
    for values := range *results {
        datas[i] = values
        i++
    }
}

The reason I do this is because the chan map[string]string there will be around millions of data inside the map and it will be more than one map.

So I think it would be a good approach if I can pass pointer to the function so that it will not copy the data to save some resource of memory.

I didn't find a good practice in effective go. So I'm kinda doubt about my approach here.

Answer

Cerise Limón picture Cerise Limón · Jun 4, 2017

It usually not good practice to use pointers to channels.

The size of a channel value is equal to the size of a pointer. The size is independent of the number of values in the channel.

You do not reduce copying by using a pointer to a channel because copying the pointer has the same cost as copying the channel.