Go language file creation and open instance analysis _golang

Source: Internet
Author: User
Tags stdin file permissions

This article 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, and is very frequently used, and familiarity with how to manipulate files is essential. Golang support for files is in the OS package, and the actions are encapsulated in the type file struct {} structure body.

One, func Open (name string) (file *file, err Error)
simply, give it a path, 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 () is a quick operation, and its prototype is as follows:

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

Second, func openfile (name string, flag int, perm FileMode) (file *file, err Error)
This complex point requires the provision of file paths, open mode, and 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: File is created (create a new file if none exists.)
O_EXCL: Used with o_create to form a new file that requires the file to be non-existent (used with o_create, file must not exist)
O_sync: Open synchronously with no caching, write directly to hard drive
O_trunc: Open and clear the file
File Permissions (Unix permission bits): required only when creating files, do not need to create a file can be set to 0. The OS library provides constants, but I generally write numbers directly, such as 0664.

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

Copy Code code 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, it is opened in read-write mode and appended to, and then opened in read-write mode if the file does not exist.

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

Copy Code code 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 Code code as follows:
Func ReadFile (PTH string) error{
F, err: = OS. Open (PTH)
If err!=nil{
return err
}
Defer f.close ()//release of resources, always remember
...
}

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 Code code as follows:
Stdin = NewFile (UIntPtr (syscall). Stdin), "/dev/stdin")
Stdout = NewFile (UIntPtr (syscall. Stdout), "/dev/stdout")
Stderr = NewFile (UIntPtr (syscall. Stderr), "/dev/stderr")

I hope this article will help you with your go language program.

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.