This is a creation in Article, where the information may have evolved or changed.
First take a look at the definition of io.copy:
Func Copyn
Func Copyn (DST Writer, Src Reader, n Int64) (written int64, err error)
Copyn copies n bytes (or until an error) from SRC to DST. It returns the number of bytes copied and the earliest error encountered while copying. On return, written = = N if and only if Err = = nil.
If DST implements the Readerfrom interface, the copy is implemented using it.
The main two parameters are DST and src:
DST is an instance of writer interface, as long as you can implement the writer interface function, you can do DST
SRC is an instance of the reader interface, as long as the reader interface functions can be done, SRC
n is the size of the content to be transferred
Two. NET required for network transmission. Definition of Conn:
Type Conn Interface { //Read reads data from the connection. //Read can made to time out and return a Error with Timeout () = = True //After a fixed time limit, see Setdeadline and Setreaddeadline. Read (b []byte) (n int, err error) //Write writes data to the connection. //Write can made to time out and return a Error with Timeout () = = True //After a fixed time limit, see Setdeadline and Setwritedeadline. Write (b []byte) (n int, err error)
Visible net. The function of writer and reader interface can be realized in Conn.
Three. Os. Definition of File
Func (*file) Read
Func (f *file) Read (b []byte) (n int, err error)
Read reads up to Len (b) bytes from the File. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to IO. Eof.
Func (*file) Write
Func (f *file) Write (b []byte) (n int, err error)
Write writes Len (b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n! = Len (b).
It is obvious that file can also be used as an example of writer and reader
Four. Definition of Os.fileinfo
Type FileInfo
Type FileInfo Interface { name () string //base Name of the file Size () Int64 //length in by TES for regular files; System-dependent for others mode () FileMode //File mode bits //modification time isdir () bool //abbreviation for Mode (). Isdir () Sys () interface{} //Underlying data source (can return nil)}
A FileInfo describes a file and is returned by Stat and Lstat.
Func Lstat
Func Lstat (name string) (FileInfo, error)
Lstat returns a FileInfo describing the named file. If The file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If There is an error, it would be of type *patherror.
Use Lstat to get the file information, where we need size ()
Five. Implementation methods
1. Building a server-side transport bridge
The server mainly plays the role of bridge, namely IO. Copyn (Net.conn, net. Conn,os. Fileinfo.size), where the first is the server and the target client link, the second is the server and the source client link, ready to pass the data, the third is to transfer the file size
2. Preparing the target client for acceptance
Use the OS first. Create creates the final file to fetch the data and then implements IO. Copyn (OS. File, net. Conn,os. Fileinfo.size ()), where OS. File is the previously created document, net. Conn is a link to the server
3. Start Transferring Data
Use the OS first. Open opens the file that needs to be transferred and then uses the OS. Lstat gets the FileInfo and then starts transmitting IO. Copyn (NET. Conn, OS. File,os. Fileinfo.size ()), where net. File is the link to the server, OS. File is the one you opened earlier.
After these three steps, network File transfer can be completed.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.