Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of writing data to a go lang channel?

Tags:

go

channel

I have a list (containers/list) containing a []string. I am sending this over a channel a lot. I am trying understand how expensive this communication is. Is the general picture that on a send a shallow copy of the data being sent is copied to the buffer and then recopied on the other side on receive? So sending and receiving is no more expensive than shallow copying? Are there some gotchas in general?

like image 713
divbyzero Avatar asked Oct 18 '25 00:10

divbyzero


1 Answers

The value is copied to and from the channel. If you are sending a container/list, then the a struct with two fields is copied. The list elements are not copied.

It's a shallow copy.

The gotcha is that the application must ensure that only one goroutine modifies the list elements.