Are channels passed by reference implicitly

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

提交回复
热议问题