Are channels passed by reference implicitly

前端 未结 4 1932
粉色の甜心
粉色の甜心 2021-01-30 06:52

The go tour has this example for channels: https://tour.golang.org/concurrency/2

package main

import \"fmt\"

func sum(a []int, c chan int) {
    sum := 0
    f         


        
相关标签:
4条回答
  • 2021-01-30 07:10

    You could say yes, but to say the "The channel c is modified in the sum function" isn't really the correct terminology. Channel sends and receives aren't really considered modifications.

    Note that slices and maps behave in a similar way, see http://golang.org/doc/effective_go.html for more details.

    Also "passed by reference" implies that an assignment could be made to c in sum that would change it's value (as opposed to it's underlying data) outside of sum, which is not the case.

    0 讨论(0)
  • 2021-01-30 07:12

    Channel variables are references, but it depends on your definition of 'reference'. Language specification never mentions reference types.

    No channel (variable) is 'modified' in the sum function. Sending to a channel changes its state.

    In other words, yes the channel is implemented as a pointer to some run time structure. Note that that's strictly necessary for the reference semantics.

    EDIT: The above sentence was meant to read: "Note that that's not strictly necessary for the reference semantics.", ie. the word 'not' went MIA. Sorry for any eventually created confusion.

    0 讨论(0)
  • 2021-01-30 07:13

    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
    }
    
    0 讨论(0)
  • 2021-01-30 07:16

    Everything in Go is passed and assigned by value. Certain built-in types, including channel types and map types, behave as opaque pointers to some hidden internal structure. And it is possible to modify that internal structure by operations on the channel or map. They start out as nil, which is analogous to the nil pointer.

    0 讨论(0)
提交回复
热议问题