zoukankan      html  css  js  c++  java
  • Go-file

    两个包具有文件操作的相关方法,一个是os,一个是syscall,其中os中的相关方法是对syscall相关方法的封装,推荐使用os中的相关方法。
    文件的打开文件的第一步操作实际上是创建,但是由于文件的打开方法也可以创建,实际中使用创建方法的地方不多。文件打开有两个方法:
    func Open(name string) (file *File, err error)//以只读方式打开一个存在的文件,打开就可以读取了。
    func OpenFile(name string, flag int, perm FileMode) (file *File, err error)//以各种方式打开各种存在不存在的文件,具体怎么样看flag和perm。

    flag可选值(掩码):
    const (
        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.
        O_APPEND int = syscall.O_APPEND // 在文件末尾追加,打开后cursor在文件结尾位置
        O_CREATE int = syscall.O_CREAT  // 如果不存在则创建
        O_EXCL   int = syscall.O_EXCL   //与O_CREATE一起用,构成一个新建文件的功能,它要求文件必须不存在
        O_SYNC   int = syscall.O_SYNC   // 同步方式打开,没有缓存,这样写入内容直接写入硬盘,系统掉电文件内容有一定保证
        O_TRUNC  int = syscall.O_TRUNC  // 打开并清空文件
    )

    perm是文件的unix权限位,可以直接用数字写,如0644。可选值有:
    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 (not backed up)
        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

        // Mask for the type bits. For regular files, none will be set.
        ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice

        ModePerm FileMode = 0777 // permission bits
    )
    上述可选值是权限位的高有效位,低有效位的值还是要用户自己写数字。

  • 相关阅读:
    leetcode 86. Partition List
    leetcode 303. Range Sum Query
    leetcode 1310. XOR Queries of a Subarray
    leetcode 1309. Decrypt String from Alphabet to Integer Mapping
    leetcode 215. Kth Largest Element in an Array
    将numpy.ndarray写入excel
    leetcode 1021 Remove Outermost Parentheses
    leetcode 1306. Jump Game III
    leetcode 1305. All Elements in Two Binary Search Trees
    ICCV2019 oral:Wavelet Domain Style Transfer for an Effective Perception-distortion Tradeoff in Single Image Super-Resolution
  • 原文地址:https://www.cnblogs.com/helww/p/4081398.html
Copyright © 2011-2022 走看看