file, _ := os.OpenFile("./output.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)

OpenFile参数说明

打开方式

 1// Flags to OpenFile wrapping those of the underlying system. Not all
 2// flags may be implemented on a given system.
 3const (
 4    // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
 5    // 只读模式
 6    O_RDONLY int = syscall.O_RDONLY // open the file read-only.
 7    // 只写模式
 8    O_WRONLY int = syscall.O_WRONLY // open the file write-only.
 9    // 可读可写
10    O_RDWR   int = syscall.O_RDWR   // open the file read-write.
11    // The remaining values may be or'ed in to control behavior.
12    // 追加内容
13    O_APPEND int = syscall.O_APPEND // append data to the file when writing.
14    // 创建文件,如果文件不存在
15    O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
16    // 与创建文件一同使用,文件必须存在
17    O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
18    // 打开一个同步的文件流
19    O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
20    // 如果可能,打开时缩短文件
21    O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
22)

打开模式

 1// The defined file mode bits are the most significant bits of the FileMode.
 2// The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
 3// The values of these bits should be considered part of the public API and
 4// may be used in wire protocols or disk representations: they must not be
 5// changed, although new bits might be added.
 6const (
 7    // The single letters are the abbreviations
 8    // used by the String method's formatting.
 9    // 文件夹模式
10    ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
11    // 追加模式
12    ModeAppend                                     // a: append-only
13    // 单独使用
14    ModeExclusive                                  // l: exclusive use
15    // 临时文件
16    ModeTemporary                                  // T: temporary file; Plan 9 only
17    // 象征性的关联
18    ModeSymlink                                    // L: symbolic link
19    // 设备文件
20    ModeDevice                                     // D: device file
21    // 命名管道
22    ModeNamedPipe                                  // p: named pipe (FIFO)
23    // Unix 主机 socket
24    ModeSocket                                     // S: Unix domain socket
25    // 设置uid
26    ModeSetuid                                     // u: setuid
27    // 设置gid
28    ModeSetgid                                     // g: setgid
29    // UNIX 字符串设备,当设备模式是设置unix
30    ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
31    // 粘性的
32    ModeSticky                                     // t: sticky
33    // 非常规文件;对该文件一无所知
34    ModeIrregular                                  // ?: non-regular file; nothing else is known about this file
35
36    // bit位遮盖,不变的文件设置为none
37    // Mask for the type bits. For regular files, none will be set.
38    ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
39    // 权限位
40    ModePerm FileMode = 0777 // Unix permission bits
41)

几种常用模式

  • os.O_WRONLY | os.O_CREATE | O_EXCL: 【如果已经存在,则失败】
  • os.O_WRONLY | os.O_CREATE: 【如果已经存在,会覆盖写,不会清空原来的文件,而是从头直接覆盖写】
  • os.O_WRONLY | os.O_CREATE | os.O_APPEND: 【如果已经存在,则在尾部添加写】

Demo

 1package main
 2
 3import (
 4    "os"
 5)
 6func main() {
 7    f, err := os.OpenFile("./output.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
 8    if err != nil {
 9        fmt.Println(err)
10    }
11    f.WriteString("current name is admin")
12    f.Close()
13
14    os.MkdirAll("./a/b/c", os.ModeDir | os.ModePerm)
15
16}