funcCopyN(dstWriter,srcReader,nint64)(writtenint64,errerror){written,err=Copy(dst,LimitReader(src,n))ifwritten==n{returnn,nil}ifwritten<n&&err==nil{// src stopped early; must have been EOF.err=EOF}return}
仅仅是加了限定的 io.Copy 方法。 在看看我们熟悉的io.Copy 方法.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funccopyBuffer(dstWriter,srcReader,buf[]byte)(writtenint64,errerror){ifwt,ok:=src.(WriterTo);ok{returnwt.WriteTo(dst)}ifrt,ok:=dst.(ReaderFrom);ok{returnrt.ReadFrom(src)}// 省略// 创建buffer// for {// read// write// }}
sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel,
sendfile() is more efficient than the combination of read(2) and write(2), which would require transferring data
to and from user space.