前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[golang] golang文件读写 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)

[golang] golang文件读写 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)

作者头像
landv
发布2020-06-18 10:56:21
4.4K0
发布2020-06-18 10:56:21
举报
文章被收录于专栏:landvlandv

[golang] golang文件读写 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)

读写文件要用到的OS包

代码语言:javascript
复制
func OpenFile(name string, flag int, perm FileMode) (*File, error) 

该方法第一个参数为文件路径,第二个参数控制文件的打开方式,第三个参数控制文件模式

可用的打开方式有

代码语言:javascript
复制
// Flags to OpenFile wrapping those of the underlying system. Not all
// flags may be implemented on a given system.
const (
    // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
    // 只读模式
    O_RDONLY int = syscall.O_RDONLY // open the file read-only.
    // 只写模式
    O_WRONLY int = syscall.O_WRONLY // open the file write-only.
    // 可读可写
    O_RDWR   int = syscall.O_RDWR   // open the file read-write.
    // The remaining values may be or'ed in to control behavior.
    // 追加内容
    O_APPEND int = syscall.O_APPEND // append data to the file when writing.
    // 创建文件,如果文件不存在
    O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
    // 与创建文件一同使用,文件必须存在
    O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
    // 打开一个同步的文件流
    O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
    // 如果可能,打开时缩短文件
    O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
)

打开模式

代码语言:javascript
复制
// 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)
    // Unix 主机 socket
    ModeSocket                                     // S: Unix domain socket
    // 设置uid
    ModeSetuid                                     // u: setuid
    // 设置gid
    ModeSetgid                                     // g: setgid
    // UNIX 字符串设备,当设备模式是设置unix
    ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
    // 粘性的
    ModeSticky                                     // t: sticky
    // 非常规文件;对该文件一无所知
    ModeIrregular                                  // ?: non-regular file; nothing else is known about this file

    // bit位遮盖,不变的文件设置为none
    // 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
)

创建一个文件并追加内容

代码语言:javascript
复制
package main

import (
    "os"
)
func main() {
    fname := "/tmp/t.txt"
    f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
    if err != nil {
        fmt.Println(err)
    }
    f.WriteString("test")
    f.Close()
}

删除文件

os.Remove(fname)

创建目录

dname :="/tmp/d" os.Mkdir(dname,os.ModeDir|os.ModePerm)

创建完整目录路径,即中间目录不存在的话也一起创建

代码语言:javascript
复制
os.MkdirAll(dname,os.ModeDir|os.ModePerm)

golang os.OpenFile几种常用模式

os.O_WRONLY | os.O_CREATE | O_EXCL           【如果已经存在,则失败】

os.O_WRONLY | os.O_CREATE                         【如果已经存在,会覆盖写,不会清空原来的文件,而是从头直接覆盖写】

os.O_WRONLY | os.O_CREATE | os.O_APPEND  【如果已经存在,则在尾部添加写】

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [golang] golang文件读写 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)
    • 读写文件要用到的OS包
      • 可用的打开方式有
        • 打开模式
          • 创建一个文件并追加内容
            • 删除文件
              • 创建目录
                • 创建完整目录路径,即中间目录不存在的话也一起创建
                • golang os.OpenFile几种常用模式
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档