Are channels passed by reference implicitly

Technically they’re copied, because when you use make, you are allocating something on the heap, so it’s technically a pointer behind the scenes. But the pointer type is not exposed, so they can be thought of as a reference type.

EDIT: From the spec:

The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values.

A channel must be initialized before it can be used. make() does this, so it can be used as a reference type.

What this basically means is that you can pass it into a function and write to or read from it. The general rule of thumb is if you use make, new or &, you can pass it to another function without copying the underlying data.

So, the following are “reference” types:

  • slices
  • maps
  • channels
  • pointers
  • functions

Only data types (numbers, bools and structs, etc) are copied when passing into a function. Strings are special, because they’re immutable, but not passed by value. This means that the following won’t work as expected:

type A struct {
    b int
}
func f(a A) {
    a.b = 3
}
func main() {
    s := A{}
    f(s)
    println(s.b) // prints 0
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)