Send slice to chan

When I send a big slice to the chan, it is sent by reference or is a full copy of the slice?

1 Like

Slices are in essence references to a backing array plus a couple of lengths, and this reference is what is passed around when you give someone a slice. This is the same whether it’s passed as a function parameter, over a channel, etc.

https://blog.golang.org/go-slices-usage-and-internals

This means the channel send is cheap. It also means that even though the slice has been sent over a channel you need to take care not to do concurrent modifications of it’s content in the two goroutines.

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.