Analysis of the creation and opening of Go language files

Source: Internet
Author: User
Tags stdin

This paper analyzes the creation and opening usage of Go language files. Share to everyone for your reference. The specific analysis is as follows:

File manipulation is a very important topic, very frequently used, and it is essential to be familiar with how to manipulate files. Golang support for files is in the OS package, which is encapsulated in the type file struct {} struct body.

I. Func Open (name string) (file *file, err Error)
Simply, give a path to it, return a file descriptor, and return a *patherror if an error occurs.
This is a read-only open mode, which is actually the OS. OpenFile () 's quick operation, it's prototype is as follows:

Copy CodeThe code is as follows: Func Open (name string) (file *file, err error) {
Return OpenFile (name, o_rdonly, 0)
}


Ii. func OpenFile (name string, flag int, perm FileMode) (file *file, err Error)
This complex point, you need to provide file path, open mode, file permissions.

Open tag:

O_rdonly: Read-only mode (read-only)
O_wronly: Write-only mode (write-only)
O_RDWR: Read/write mode (read-write)
O_append: Append mode (APPEND)
O_create: The file does not exist and is created (create a new file if none exists.)
O_EXCL: Used with o_create to form a new file feature that requires that the file must not exist (used with o_create, file must not exist)
O_sync: Synchronous open, that is, do not use the cache, directly write to the hard disk
O_trunc: Open and empty files
File Permissions (Unix permission bit): Required only when creating a file, you do not need to create a file that can be set to 0. Although the OS Library provides constants, I generally write numbers directly, such as 0664.

If you need to set multiple open tags and UNIX permission bits, you need to use the bitwise operator "|", the sample code is as follows:

Copy CodeThe code is as follows: F, err: = OS. OpenFile ("Test.txt", OS. O_create|os. O_append|os. O_RDWR, OS. Modeperm|os. Modetemporary)
If err! = Nil {
Panic (ERR)
}


If the file exists, open in read-write mode, append write, create if the file does not exist, and open in read-write mode.

Third, func Create (name string) (file *file, err Error)
This is actually the OS. Quick action for OpenFile (). Creates a new file and opens it in read-write mode, with the permission bit "0666" and emptied if the file exists. The prototype is as follows:

Copy CodeThe code is as follows: Func Create (name string) (file *file, err error) {
Return OpenFile (name, o_rdwr| o_create| O_trunc, 0666)
}


Iv. open operation of any file, please remember to release it in time

Copy CodeThe code is as follows: Func ReadFile (PTH string) error{
F, err: = OS. Open (PTH)
If err!=nil{
return err
}
Defer f.close ()//release resources, always do not forget
...
}


The OS module also has a func NewFile (fd uintptr, name string) *file function that creates a file using the given UNIX file descriptor and name. Reference:

Copy CodeThe code is as follows: Stdin = NewFile (UIntPtr (syscall. Stdin), "/dev/stdin")
Stdout = NewFile (UIntPtr (syscall. Stdout), "/dev/stdout")
Stderr = NewFile (UIntPtr (syscall. Stderr), "/dev/stderr")

Analysis of the creation and opening of Go language files

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.