Go language file operations

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

absrtact: This paper mainly explains the file operation of Go language. System Open\write, such as system call, often manipulate the file object is the file description of the descriptor, while the C language library file operations need to use Fopen/fread functions, Their operands are file pointers . In the Go language, file operations are further encapsulated ...

File class in 1.os package

First, the file class is in the OS package, encapsulating the underlying document descriptor and related information, encapsulating the read and write implementations at the same time.

typestruct {    *file}typestruct {    fd  int    string    dirinfo *dirInfo    nepipe  int}func (f *File) Fd( )uintptr{    ifnil{        return ^(uintptr(0))    }    returnuintptr(f.fd)}

Notice why you can use F.FD in this syntax, F is file, and file does not have a member?

func (f *File) Close() error{}func (f *File)Stat ()(fi FileInfo, err error){}

At the same time, the file class implements such methods as the following:

func (f *File) read(b []byteint, err error);func (f *File) write(b []byteint, err error) ;funcint64intint64, err error) ;

2.io. Readcloser

In the go language, with the ER suffix is often the interface, Readcloser as the name implies, including the read and Close method excuses. Let's take a look at the definitions and implementations of these two interfaces:

type Reader interface {    Read(p []byte) (n int, err error)}type Closer interface {    Close() error}

Note: The definition of reader in different packages is different, the following is the definition of reader in Bufio

3. Bufio Package

typestruct {    // contains filtered or unexported fields}typestruct {            buf          []byte            rd           io.Reader            r, w         int            err          error            lastByte     int            int}

Bufio is a buffered IO read-write package, let's take a look at the example:

     PackageMainImport("FMT"        "OS"        "Bufio"        "IO")funcMain () {f, err: = OS. Open ("C:\\aaa.txt")//Open File        deferF.close ()//Open File error handling        if Nil= = Err {buff: = Bufio. Newreader (f)//Read in cache             for{line, err: = Buff. ReadString (' \ n ')//Read a line with ' \ n ' for Terminator                ifErr! =Nil|| Io. EOF = = Err { Break} FMT. Print (line)//ability to process a row}        }    }

Func Newreader (rd IO. Reader) *reader
Newreader returns a new Reader whose buffer has the default size.

4. Example

type Request struct {    // The message body.    ...    Body io.ReadCloser    ...}

So, assuming we want to implement the body in request, we just need to implement the two interface in the IO package.

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.