Are channels passed by reference implicitly

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

提交回复
热议问题