英文:
Cost of writing data to a go lang channel?
问题
我有一个包含[]string
的list
(containers/list
)。我经常通过通道发送它。我试图了解这种通信的开销有多大。一般情况下,在发送时,被发送的数据的浅拷贝会被复制到缓冲区,然后在接收时再次复制。所以发送和接收的开销不会比浅拷贝更大。一般情况下,是否存在一些需要注意的地方?
英文:
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?
答案1
得分: 6
该值被复制到通道中,并从通道中复制出来。如果你发送的是一个容器/列表,那么会复制一个具有两个字段的结构体。列表元素不会被复制。
这是一种浅拷贝。
需要注意的是,应用程序必须确保只有一个goroutine修改列表元素。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论