What are the benefits of golang channel

(*-*)浩
Release: 2019-12-03 09:47:03
Original
3164 people have browsed it

What are the benefits of golang channel

#The advantage of golang channel is that it avoids complex locking mechanisms under race conditions by providing atomic communication primitives.

channel

A channel is a communication mechanism that allows one goroutine to send specific values ​​to another gouroutine. (Recommended study: go)

can be understood as a conduit for transmitting a certain type of value, and the type passed in the channel becomes the element type of the channel.

A reference to a data structure created using make. When channel is used as a parameter, it is actually the zero value of the

channel call by reference: nil

Channel can be regarded as a FIFO queue. Reading and writing to the FIFO queue are atomic operations and do not require locking. The results of the operation behavior of the channel are summarized as follows:

What are the benefits of golang channel

#When reading a closed channel, the zero value of the corresponding type can always be read. In order to The difference in behavior of reading non-null unclosed channels can be achieved by using two receiving values:

// ok is false when ch is closedv, ok := <-ch
Copy after login

Most types in golang are value types (only slice/channel/map are reference types), read/write types When it is a value type channel, if the element size is relatively large, pointers should be used instead to avoid frequent memory copy overhead.

The above is the detailed content of What are the benefits of golang channel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template