Junior — Middle
How to determine if a data stream or channel has finished its work during reading?
sobes.tech AI
Answer from AI
In Go, when reading from a stream (for example, from io.Reader), the end of data or closing of the channel is indicated by the returned error io.EOF. This is the standard way to understand that the stream has finished.
Example:
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err == io.EOF {
// stream has ended
} else if err != nil {
// handle other errors
} else {
// process the read data
}
Thus, checking for io.EOF allows you to determine that the stream or channel has completed its operation during reading.