golang Read(p [] byte)不读取完整字节吗?

Recently I use golang Read(p []byte),intending to read full len(p) bytes. However I find that Read does not guarantee read len(p) bytes. That's, I need read 4 bytes but actually it gives me only 1. At last I use io.ReadFull instead.

Now I am confused, what's the meaning of the function? What's the proper scene using Read? It may just read less bytes than you need.

If you check the documentation and the source code, you will realize why the bufio.Read(p []byte) does not guarantee the full read of data into p Reader.

Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying Reader, hence n may be less than len(p).

Copying from the source code, at the end of the function there is a copy operation where the buffer is copied into the byte array. But this does not guarantee that the full chunk of data is copied.

// copy as much as we can
n = copy(p, b.buf[b.r:b.w]) // => this line is important
b.r += n
b.lastByte = int(b.buf[b.r-1])
b.lastRuneSize = -1
return n, nil

If you wish to copy the full length of bytes use bufio.ReadBytes instead, using EOF or EOL delimiter as parameter, depending on your use case.