Are channels passed by reference implicitly

前端 未结 4 2032
粉色の甜心
粉色の甜心 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: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.

提交回复
热议问题