OS packages for Go
Article directory
1. The role of OS packages
The API in the os package mainly helps us use the file system, permission system, environment variables, system processes, and system signals in the operating system.
For Unix-like operating systems, everything in it can be regarded as a file. So there os.File
are many things that can be manipulated with types.
This article applies os.File
the type to regular files.
2. Which package interfaces os.File
are implemented by the typeio
os.File
All it has are pointer methods, so it itself doesn't implement any interfaces other than the empty interface. And its pointer type implements many interfaces in the io code package.
-
*os.File
Implemented the three most core simple interfaces in the io package:io.Reader
,io.Writer
,io.Closer
; -
*os.File
Three other simple interfaces are also implemented:io.ReaderAt
,io.Seeker
,io.WriterAt
; -
Since the above six simple interfaces are realized, it also realizes 7 of the
*os.File
9 extended interfaces in the io package:io.ReadWriter
, ,io.ReadCloser
,io.ReadSeeker
,io.ReadWriteCloser
,io.ReadWriteSeeker
,io.WriteCloser
.io.WriteSeeker
Since ·
*os.File
does not implement the simple interfaceio.ByteReader
andio.RuneReader
, it does not implement it as an extension of both of its interfaces:io.ByteScanner
andio.RuneScanner
.
Therefore, os.File
you can read and write files, and read and set the starting index position of the next read and write. Files can also be closed. However, it does not specifically read the next byte of the file, or write a Unicode character, nor do any read rollbacks.
3. Create os.File
the pointer type of the type
In the os package, there are several functions: Create, NewFile, Open and OpenFile.
3.1 os.Create
os.Create
The function is used to create a new file according to the specified path.
Files created using this function are readable and writable by all users of the operating system.
- If
os.Create
a file already exists at the path we give the function, then the function will clear all the contents of the existing file before returning it as the first result value; - If a level-parent directory of the given path does not exist, the function returns an
*os.PathError
error value of type "non-existent file or directory".
3.2 os.NewFile
When this function is called, it needs to accept a value of type uintptr representing the file descriptor and a character value representing the file name.
- If the given file descriptor is not valid, the function will return nil. Otherwise, it will return a File value representing the corresponding file;
The function of this function is not to create a new file, but to create a new File value that wraps the file based on an existing file descriptor.
3.3 os.Open
os.Open
The function opens a file and returns a File value that wraps the file. However, this function can only open files in read-only mode.
In other words, we can only read from the File value returned by this function, but not write anything to it.
If you call any of its write methods, you will get an error value indicating "bad file descriptor".
os.File
The type has a pointer method Fd, which will return a value of type uintptr after being called, and this value represents the file descriptor held by the current File value.
3.4 os.OpenFile
os.OpenFile
In fact os.Open
, it is os.Create
the underlying support of the function, which is the most flexible.
This function has 3 parameters:
- name: file path;
- flag: The mode that needs to be applied on the file descriptor; for example, read-only mode (by
os.O_RDONLY
). operating mode - perm: It also represents the mode, and the type
os.FileMode
is a redefined type based on the uint32 type. permission mode.
The operation mode defines the way of the file, and the permission mode controls the access rights of the file.
4. os.File
What are the operation modes of the value
The operation modes for File values mainly include: read-only mode, write-only mode, and read-write mode.
- Read-only mode:
os.O_RDONLY
- Write-only mode:
os.O_WRONLY
- Read and write mode:
os.O_RDWR
More operating modes:
os.O_APPEDN
: The writing content is appended to the existing content;os.O_CREATE
: Create a new file when the file at the given path does not exist;os.O_EXCL
: It needs toos.O_CREATE
be used together with , indicating that no existing files can exist in the given path.os.O_SYNC
: Implements synchronous I/O on top of open files. It will ensure that the content read and written is always synchronized with the data on the hard disk.os.O_TRUNC
: If the file already exists, and is a regular file, any existing content in it is first cleared.
|
Multiple modes of operation are combined using bitwise operators .
func Create(name string) (*File, error) {
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
}
func Open(name string) (*File, error) {
return OpenFile(name, O_RDONLY, 0)
}
Five, os.OpenFile
the third parameter that represents the mode
os.OpenFile
The third parameter perm of the function represents the permission mode, and its type is os.FileMode
. In fact, os.FileMode
it can represent not only permission mode, but also file mode (also called file type).
os.FileMode
It is a redefined type based on the uint32 type, including 23 bits, and among these 23 bits, each bit has a specific meaning.
- If the binary number in the most significant bit is 1, the file mode is equivalent
os.ModeDir
, that is, the corresponding file is a directory. - If the binary number in bit 26 is 1, the file mode is equivalent to
os.ModeNamedPipe
, that is, the file is a named pipe.
os.FileMode
Only the lowest 9 bits are used to indicate file permissions.
Take os.FileMode
the value of the type and os.ModePerm
the constant (the value is 0777) to perform a bitwise AND operation, and the obtained value indicates the corresponding permission mode.
These 9 bits, 3 as a group, a total of 3 groups. From high to low, respectively represent: the file owner (the user who created the file), the user group to which the file belongs, and the user's access rights to the file. And for each group, the 3 bits in it respectively represent from high to low: read permission, write permission, and execute permission.
If a certain bit is 1, it means that the corresponding permission is turned on, otherwise, it means that the corresponding permission is turned off.
- The octal integer 0777 means: all users in the operating system have permission to read, write and execute the current file;
- The octal integer 0666 means: all users have read and write permissions to the current file, but none have execution permissions.
But note that the third parameter value here is valid only when creating a new file . In other cases, even if we set this parameter, it will not have any effect on the target file.