Using pointer to channel

Viewed 16481

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.

3 Answers

Everything in Golang is passed by value. Even pointers are a type and assigned the value of the memory address. So they are values too.

(Extending Rick's answer) There are actually six types that hold pointer values and a pointer to these (i.e. a pointer to a pointer) types doesn't help anyway:

  1. pointers
  2. slices
  3. maps
  4. channels
  5. interfaces
  6. function
Related