GO language file creation and open instance analysis

  • 2020-05-09 18:43:45
  • OfStack

This article illustrates the creation and opening of GO language files. Share with you for your reference. The specific analysis is as follows:

File manipulation is a very important and frequent topic, and it is essential to be familiar with how to manipulate files. Golang's support for files is in os package, and the operations are encapsulated in the type File struct {} structure.

1. func Open(name string) (file *File, err error)
Simply give it a path, return the file descriptor, and if something goes wrong, return a *PathError.
This is a read-only open mode, which is actually a shortcut to os.OpenFile (). The prototype is as follows:

func Open(name string) (file *File, err error) {
    return OpenFile(name, O_RDONLY, 0)
}

2. func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
For this complexity, you need to provide the file path, open mode, and file permissions.

Open mark:

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: create a file if it doesn't exist (create a new file if none exists.)
O_EXCL: functions as a new file with O_CREATE 1, which requires that the file does not exist (used with O_CREATE, file must not exist)
O_SYNC: synchronous mode on, that is, without the use of cache, directly write to the hard disk
O_TRUNC: open and empty the file
File permissions (unix permission bit) : only required when creating a file, no need to create a file can be set to 0. The os library provides constants, but I write Numbers directly, like 0664.

If you need to set multiple open flags and unix permission bits, you need to use the bit operator "|". The sample 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 it in read-write mode and append it to write. If the file does not exist, create it and open it in read-write mode.

3. func Create(name string) (file *File, err error)
This is actually a shortcut to os.OpenFile (). Create a new file and open it in read/write mode with the permission bit "0666". If the file exists, it will be cleared. The prototype is as follows:

func Create(name string) (file *File, err error) {
    return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
}

4. Any file opening operation, please remember to release in time
func ReadFile(pth string) error{
 f, err := os.Open(pth)
    if err!=nil{
        return err
    }
 defer f.Close() // Release resources and never forget them
    ...
}

There is also one func NewFile(fd uintptr, name string) *File function in the os module, which creates a file with the given Unix file descriptor and name. Reference:
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 has been helpful to you in the programming of GO language.


Related articles: