File操作
# File文件操作
file
类是在os
包中的,封装了底层的文件描述符和相关信息,同时封装了Read
和Write
的实现
# 一、FileInfo接口
FileInfo
: https://golang.google.cn/pkg/os/#FileInfoFileInfo
接口中定义了File
信息相关的方法.
type FileInfo interface {
Name() string // base name of the file 文件名,扩展名 aa.txt
Size() int64 //文件大小,字节数 12540
Mode() FileMode // 文件权限 -rw-rw-rw-
ModTime() time.Time // 修改时间 2018-04-13 16:30:53 +0000 CST
IsDir() bool //是否文件夹
Sys() interface{} //基础数据源接口{can return nil}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 二、权限
至于操作权限perm
,除非创建文件时才需要指定,不需要创建新文件时可以将其设定为0
.虽然go
语言给perm
权限设定了很多常量,但是习惯上也可以使用数字,如666
(具体含义和Unix
系统的一致).
权限控制:
linux 下有2中文件权限表示方式,即"符号表示"和"八进制表示"
(1) 符号表示方式:
-- --- --- ---
type owner group others
文件的权限是这样子分配的 读 写 可执行 分别对应的是 r w x 如果没有一个权限,用 - 代替
(-文件 d目录 |连接符号)
例如: -rwxr-xr-x
(2) 八进制表示方法:
r ---> 004
w ---> 002
x ---> 001
- ---> 000
0755
0777
0555
0444
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 三、打开模式
文件打开模式:
const {
O_RDONLY int = syscall.O_RDONY // 只读模式打开文件
O_WRONLY int = syscall.O_WRONLY //只写模式打开文件
O_RDWR int = syscall.O_RDWR //读写模式打开文件
O_APPEND int = syscall.O_APPEND //写操作时将数据附加到文件尾部
O_CREATE int = syscall.O_CREATE //如果不存在将创建一个新文件
O_EXCL int = syscall.O_EXCL //和`O_CREATE`配置使用,文件必须不存在
O_SYNC int = syscall.O_SYNC //打开文件用于异步I/O
O_TRUNC int = syscall.O_TRUNC //如果核能,打开时清空文件
}
type FileMode uint32
// The defined file mode bits are the most significant bits of the FileMode.
// The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
// The values of these bits should be considered part of the public API and
// may be used in wire protocols or disk representations: they must not be
// changed, although new bits might be added.
const (
// The single letters are the abbreviations
// used by the String method's formatting.
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
ModeAppend // a: append-only
ModeExclusive // l: exclusive use
ModeTemporary // T: temporary file; Plan 9 only
ModeSymlink // L: symbolic link
ModeDevice // D: device file
ModeNamedPipe // p: named pipe (FIFO)
ModeSocket // S: Unix domain socket
ModeSetuid // u: setuid
ModeSetgid // g: setgid
ModeCharDevice // c: Unix character device, when ModeDevice is set
ModeSticky // t: sticky
ModeIrregular // ?: non-regular file; nothing else is known about this file
// Mask for the type bits. For regular files, none will be set.
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
ModePerm FileMode = 0777 // Unix permission bits
)
1
2
3
4
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
2
3
4
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
# 四、File操作
type File
// File代表一个打开的文件对象.
func Create(name string)(file *File,err error)
// Create采用模式`0666`(任何人都可以读写,不可执行)创建一个名为`name`的文件,如果文件已存在就会截断它(为空文件).如果成功,返回的文件对象可用于`I/O`对应的文件描述符具有`O_RDWR`模式.如果出错,错误底层类型是`*PathError`.
func Open(name string)(file *File,err error)
//`Open`打开一个文件用于读取,如果操作成功,返回的文件对象的方法可用于读取数据;对应的文件描述符具有`O_RDONLY`模式,如果出错,错误底层类型是`*PathError`
func OpenFile(name string,flag int,perm FileMode) (file *File,err error)
//`OpenFile`是一个更一般性的文件打开函数,大多数调用者都应用`Open`或`Create`代替本函数.它会使用指定的选项(如`O_RDONLY`等)、指定的模式(如`0666`等)打开指定名称的文件.如果操作成功,返回的文件对象可用于`I/O`.如果出错,错误底层类型是`*PathError`
func NewFile(fd uintptr, name string) *File {
//`NewFile`使用给出的`Unix`文件描述符和名称创建一个文件.
h := syscall.Handle(fd)
if h == syscall.InvalidHandle {
return nil
}
return newFile(h, name, "file")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2024/04/09, 16:48:42
- 01
- AWS NAT-NetWork-Firwalld配置(一)04-09
- 02
- AWS NAT-NetWork-Firwalld配置(二)04-09
- 03
- kubernetes部署minio对象存储01-18