Golang source Read OS. File

Source: Internet
Author: User
Tags readfile
This is a creation in Article, where the information may have evolved or changed.

The recent writing process feels Golang reading and writing files is slow. So decided to read the source code.

Src/os/file.go

Http://www.ieyebrain.com:8080/golang/src/os/file.go

The function for file is defined in:

Name, Read,write,seek,close, and so on.

For example, the Read function

Func (f *file) Read (b []byte) (n int, err error) {
if f = = nil {
return 0, Errinvalid
}
N, E: = F.read (b)
if n = = 0 && len (b) > 0 && E = = Nil {
return 0, Io. Eof
}
If E! = Nil {
Err = &patherror{"read", F.name, E}
}
return N, err
}

This implements the interface technique for delegate invocation. is to delegate the read operation to the F.read function. F is a pointer to the file type, and a circle is found, only to find that it is defined in the implementation-specific file. For example: in File_windows.go,

Http://www.ieyebrain.com:8080/golang/src/os/file_windows.go

Type File struct {
*file
}
Type file struct {
FD Syscall. Handle
Name string
Dirinfo *dirinfo//nil unless directory being read
L Sync. Mutex//used to implement Windows Pread/pwrite

Only for console IO
isconsole BOOL
lastbits []byte//first few bytes of the last incomplete rune in last write
READBUF []rune//Input Console buffer
}

The following is the read main, Syscall. Read (f.fd,b). WindowsAPI, Syscall, file, file---

Three more calls, file---file is implemented across platforms. File This layer adds a lock. This could be a big drain. This lock is for unified operation semantics and does not have this lock on UNIX platforms.

Func (f *file) read (b []byte) (n int, err error) {
F.l.lock ()
Defer F.l.unlock ()
If F.isconsole {
return F.readconsole (b)
}
Return Fixcount (Syscall. Read (F.FD, B))
}

Call Procedure:

1 http://www.ieyebrain.com:8080/golang/src/syscall/syscall_windows.go

Func Read (FD Handle, P []byte) (n int, err error), ReadFile

2 Http://www.ieyebrain.com:8080/golang/src/syscall/zsyscall_windows.go

Func ReadFile (handle handle, buf []byte, Done *uint32, overlapped *overlapped) (err Error)

3 Http://www.ieyebrain.com:8080/golang/src/runtime/syscall_windows.go

Func syscall_syscall6 (FN, Nargs, A1, A2, A3, A4, A5, A6 uintptr) (R1, R2, err UIntPtr) {
c: = &GETG (). M.syscall
C.Fn = fn
C.N = Nargs
C.args = UIntPtr (Noescape (unsafe. Pointer (&A1)))
Cgocall (asmstdcalladdr, unsafe. Pointer (c))
Return C.R1, C.R2, C.err
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.