divbyzero
divbyzero

Reputation: 323

Cost of writing data to a go lang 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?

Upvotes: 4

Views: 1342

Answers (1)

Thundercat
Thundercat

Reputation: 120979

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.

Upvotes: 6

Related Questions