One of the Golang file operations

Source: Internet
Author: User
Tags symlink
This is a creation in Article, where the information may have evolved or changed.
go to the official library of file operations scattered in a number of packages, feeling a bit messy, for example os , ioutil package, based on the summary of others on the basis of a simple summary

1. Basic file operation

1.1 Creating an empty file

  package mainimport (    "os"    "log")var (    newFile *os.File    err error)func main(){      //创建文件      newFile,err=os.Create("text.txt")      checkErr(err)      log.Println(newFile)      newFile.Close()}/** 检查错误 */func checkErr(err error){      if err!=nil{          log.Fatal(err)      }}

1.2 Truncate file

     package main        import (        "log"        "os"    )    func main(){        // 裁剪一个文件到100个字节。        // 如果文件本来就少于100个字节,则文件中原始内容得以保留,剩余的字节以null字节填充。        // 如果文件本来超过100个字节,则超过的字节会被抛弃。        // 这样我们总是得到精确的100个字节的文件。        // 传入0则会清空文件。        err:=os.Truncate("text.txt",100)        checkErr(err)    }        func checkErr(err error){        if err!=nil{            log.Panic(err)        }    }

1.3 Getting file information

    package mainimport (    "log"    "os"    "fmt")var (    fileInfo os.FileInfo    err      error)func main() {    fileInfo, err = os.Stat("text.txt")    checkErr(err)    fmt.Println("FIle name:", fileInfo.Name())    fmt.Println("Size in bytes:", fileInfo.Size())    fmt.Println("Permissions:", fileInfo.Mode())    fmt.Println("Last modified:", fileInfo.ModTime())    fmt.Println("Is directory:", fileInfo.IsDir())    fmt.Printf("System interface type:%T\n", fileInfo.Sys())    fmt.Printf("System info:%+v\n\n", fileInfo.Sys())}func checkErr(err error) {    if err != nil {        log.Panic(err)    }}

Run results

1.4 Renaming and moving

rename move Same as the principle
.
package mainimport (    "log"    "os")func main() {    oldPath, newPath := "text.txt", "test.txt"    err := os.Rename(oldPath, newPath)    checkErr(err)}func checkErr(err error) {    if err != nil {        log.Panic(err)    }}

1.5 Deleting files

    package mainimport (    "log"    "os")func main() {    err := os.Remove("text.txt")    checkErr(err)}func checkErr(err error) {    if err != nil {        log.Panic(err)    }}

1.6 Open File

    package mainimport (    "log"    "os")func main() {    // 简单地以只读的方式打开。下面的例子会介绍读写的例子。    file,err:=os.Open("test.txt")    checkErr(err)    file.Close()    // OpenFile提供更多的选项。    // 最后一个参数是权限模式permission mode    // 第二个是打开时的属性    file1,err:=os.OpenFile("hello.txt",os.O_CREATE|os.O_APPEND,0666)    checkErr(err)    file1.Close()    //下面的属性可以单独使用,也可以组合使用。    // 组合使用时可以使用 OR 操作设置 OpenFile的第二个参数,例如:    // os.O_CREATE|os.O_APPEND    // 或者 os.O_CREATE|os.O_TRUNC|os.O_WRONLY    // os.O_RDONLY // 只读    // os.O_WRONLY // 只写    // os.O_RDWR // 读写    // os.O_APPEND // 往文件中添建(Append)    // os.O_CREATE // 如果文件不存在则先创建    // os.O_TRUNC // 文件打开时裁剪文件    // os.O_EXCL // 和O_CREATE一起使用,文件不能存在    // os.O_SYNC // 以同步I/O的方式打开}func checkErr(err error) {    if err != nil {        log.Panic(err)    }}

1.7 Checking if a file exists

    package mainimport (    "log"    "os")func main() {    fileInfo,err:=os.Stat("hello.txt")      if err!=nil{          if os.IsNotExist(err){              log.Fatalln("file does not exist")        }      }      log.Println("file does exist. file information:")      log.Println(fileInfo)}

1.8 Checking Read and Write permissions

    package mainimport (    "os"    "log")func main() {    // 这个例子测试写权限,如果没有写权限则返回error。    // 注意文件不存在也会返回error,需要检查error的信息来获取到底是哪个错误导致。    file, err := os.OpenFile("1.txt", os.O_WRONLY, 0666)    if err != nil {        if os.IsPermission(err) {            log.Println("Error write permission denied")        }        if os.IsNotExist(err) {            log.Println("file does not exist")        }    }    file.Close()    // 测试读权限    file, err = os.OpenFile("test.txt", os.O_RDONLY, 0666)    if err != nil {        if os.IsPermission(err) {            log.Println("Error:Read permission denied")        }    }    file.Close()}

1.9 Changing permissions, owners, timestamps

package mainimport (    "os"    "log"    "time"    "fmt")func main(){    // 使用Linux风格改变文件权限    err:=os.Chmod("test.txt",0777)    checkErr(err)    // 改变文件所有者    err=os.Chown("test.txt",os.Geteuid(),os.Getgid())    checkErr(err)    twoDaysFromNow:=time.Now().Add(48*time.Hour)    lastAccessTime:=twoDaysFromNow    lastModifyTime:=twoDaysFromNow    err=os.Chtimes("test.txt",lastAccessTime,lastModifyTime)    checkErr(err)    fileInfo,err:=os.Stat("test.txt")    fmt.Println("file modified time:",fileInfo.ModTime())    }func checkErr(err error){    if err!=nil{        log.Println(err)    }}

1.10 Hard links and soft links

an ordinary file is a place that points to the inode of the hard disk.
A hard link creates a new pointer pointing to the same place. The file will not be deleted until all links have been deleted. Hard links work only in the same file system. You can think of a hard link as a normal link.

Symbolic link, also known as soft connection, and hard link is a bit different, it does not directly point to the same place in the hard disk, but by name to refer to other files. They can point to different files in different file systems. Not all operating systems support soft links.

package mainimport (    "os"    "log"    "fmt")func main() {    // 创建一个硬链接。    // 创建后同一个文件内容会有两个文件名,改变一个文件的内容会影响另一个。    // 删除和重命名不会影响另一个。    err := os.Link("original.txt", "original_also.txt")    if err != nil {        log.Fatal(err)    }    fmt.Println("creating sym")    // Create a symlink    err = os.Symlink("original.txt", "original_sym.txt")    if err != nil {        log.Fatal(err)    }    // Lstat返回一个文件的信息,但是当文件是一个软链接时,它返回软链接的信息,而不是引用的文件的信息。    // Symlink在Windows中不工作。    fileInfo, err := os.Lstat("original_sym.txt")    if err != nil {        log.Fatal(err)    }    fmt.Printf("Link info: %+v", fileInfo)    //改变软链接的拥有者不会影响原始文件。    err = os.Lchown("original_sym.txt", os.Getuid(), os.Getgid())    if err != nil {        log.Fatal(err)    }}

Article Source: http://colobu.com/2016/10/12/...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.