Home > Backend Development > Golang > Will golang read block?

Will golang read block?

(*-*)浩
Release: 2019-12-17 10:34:25
Original
4596 people have browsed it

Will golang read block?

#The Reader interface in the Go language defines a Read operation, and anyone who implements the Reader interface has the Read function.                                                                                                                                                                                         p) bytes of data into buffer p

n indicates the actual number of bytes read this time, (0 <= n <= len(p))

err Indicates the error report of this read operation (if any, nil if not)
Even if the number of bytes read n is less than len(p), the calling body may also use it during the function call. All the space of p (I don’t know what this description is trying to express)

When reading data, even if it is not enough len(p) (there is no data in the IO cache at this time), the Read operation will usually be performed directly Return the obtained data instead of continuing to wait for more data to arrive in the IO buffer

When the Read operation encounters an error or the end of the file after successfully reading n bytes of data, it will Will return the n bytes of data read, and then the return of err may have two operations:

Return n (>0) and err (non-nil) during this Read operation

Or this Read operation returns n (>0) and nil, and the next call returns 0 and err (non-nil)

Both operations are possible, so The caller should always first determine whether there is data returned according to n>0, instead of first determining whether err is nil

For example, when a certain call reaches the end of the file and also reads When n bytes of data are required, Reader may be implemented in two ways:

The current call returns n and EOF error

The current call returns n and nil, and the next call returns 0 And the implementation of EOF

Read should not return 0 and nil, that is, when returning 0, it should return a non-nil error, unless the size of the buffer space is 0, then the caller should ignore calling 0 and Return value combinations such as nil are treated as if nothing happened. It is particularly emphasized that this return does not indicate EOF

The above is the detailed content of Will golang read block?. 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