“This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022”
First, the File class is in the OS package and encapsulates the underlying file descriptor and related information, as well as the Read and Write implementations.
1. The FileInfo interface
The FileInfo interface defines methods related to File information.
type FileInfo interface {
Name() string
Size() int64
Mode() FileMode
ModTime() time.Time
IsDir() bool
Sys() interface{}}Copy the code
2. The permissions
As for the operation permission perm, it needs to be specified only when creating a file. It can be set to 0 when no new file needs to be created. Although the GO language gives perm permissions a lot of constants, it is customary to use numbers such as 0666.
Permission control:
Linux under2Representation of file permissions, namely "symbolic representation" and "octal representation". (1) symbol: - --- --- ---typeOwner group others file permissions are that the distribution of read write executable corresponding r w x if without a permission, use - instead of d (- file directory | connection symbol), for example: - rwxr-xr-x mto (2) octal notation: r -- >004W - >002X - >001-- - >000
0755
0777
0555
0444
0666
Copy the code
3. Open the mode
File opening mode:
const (
O_RDONLY int = syscall.O_RDONLY
O_WRONLY int = syscall.O_WRONLY
O_RDWR int = syscall.O_RDWR
O_APPEND int = syscall.O_APPEND
O_CREATE int = syscall.O_CREAT
O_EXCL int = syscall.O_EXCL
O_SYNC int = syscall.O_SYNC
O_TRUNC int = syscall.O_TRUNC
)
Copy the code
4. The File operations
type File
func Create(name string) (file *File, err error)
func Open(name string) (file *File, err error)
func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
func NewFile(fd uintptr, name string) *File
func Pipe(a) (r *File, w *File, err error)
func (f *File) Name(a) string
func (f *File) Stat(a) (fi FileInfo, err error)
func (f *File) Fd(a) uintptr
func (f *File) Chdir(a) error
func (f *File) Chmod(mode FileMode) error
func (f *File) Chown(uid, gid int) error
func (f *File) Close(a) error
func (f *File) Readdir(n int) (fi []FileInfo, err error)
func (f *File) Readdirnames(n int) (names []string, err error)
func (f *File) Truncate(size int64) error
Copy the code
5. Sample code
File information: FileInfo
package main
import (
"os"
"fmt"
)
func main(a) {
fileInfo,err := os.Stat("/Users/ruby/Documents/pro/a/aa.txt")
iferr ! =nil{
fmt.Println("err :",err)
return
}
fmt.Printf("%T\n",fileInfo)
fmt.Println(fileInfo.Name())
fmt.Println(fileInfo.Size())
fmt.Println(fileInfo.IsDir())
fmt.Println(fileInfo.ModTime())
fmt.Println(fileInfo.Mode())
}
Copy the code
File operation example:
package main
import (
"fmt"
"path/filepath"
"path"
"os"
)
func main(a) {
fileName1:="/Users/ruby/Documents/pro/a/aa.txt"
fileName2:="bb.txt"
fmt.Println(filepath.IsAbs(fileName1))
fmt.Println(filepath.IsAbs(fileName2))
fmt.Println(filepath.Abs(fileName1))
fmt.Println(filepath.Abs(fileName2))
fmt.Println("Get parent directory:",path.Join(fileName1,".."))
err := os.RemoveAll("/Users/ruby/Documents/pro/a/cc")
iferr ! =nil{
fmt.Println("err:",err)
return
}
fmt.Println("Directory deletion succeeded.")}Copy the code
12 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 109 110 111 112 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
Copy the code