A collation of common functions in Golang's os standard library

  • 2020-05-30 20:22:06
  • OfStack

os.Rename () the prototype of this function is func Rename(oldname, newname string) error, the old file name is entered, the new file name is entered, and an error is returned, syscall.Rename () is then renamed by MoveFile(from *uint16, to *uint16) (err error) = MoveFileW


 import (
 "fmt"
 "os"
) func main() {
 err := os.Rename("1.go", "2.go")
 if err != nil {
  if os.IsExist(err) { // judge 1 There is an error in the file
   fmt.Println(" The file already exists ")
   os.Rename("1.go", "widuu_1.go")
  }
 }
}

os.SameFile () is a function that detects if the file information is the same as os.Stat (), and the prototype is func SameFile(fi1, fi2 FileInfo) bool
For example

import (
 "fmt"
 "os"
) func main() {
 f1, _ := os.Stat("1.go")
 f2, _ := os.Stat("21.go")
 if os.SameFile(f1, f2) {
  fmt.Println(" Two files 1 sample ")
  return
 }
 fmt.Println(" Two files don't 1 sample ")
}

os.Setenv () this function is very simple to set the environment variable, the function prototype func Setenv(key, value string) error input the corresponding key-value string, return error information

import (
 "fmt"
 "os"
) func main() {
 err := os.Setenv("WD_PATH", "D:/golang")
 if err != nil {
  fmt.Println(err)
 }
 env := os.Getenv("WD_PATH")
 fmt.Println(env) // Returns the D:/golang
}

os.Symlink () for this function I can only say that windows platform is not supported, create a soft connection func Symlink(oldname, newname string) error

import (
 "fmt"
 "os"
) func main() {
 err := os.Symlink("1.go", "21.go") // Does not support windows Platform support only linux and unix
 fmt.Println(err)
}

os.TempDir () this function is very simple, return your local system temp directory, function prototype func TempDir() string, hey, do a comparison don't mess up

import (
 "fmt"
 "io/ioutil"
 "os"
) func main() {
 // Create temporary tmp
 dir, _ := os.Getwd()
 path, _ := ioutil.TempDir(dir, "tmp")
 fmt.Println(path) //D:\test\tmp764030415
 // This is going to return the system temp
 temp := os.TempDir()
 fmt.Println(temp) //windows for C:\Users\ADMINI~1\AppData\Local\Temp
}

os.Truncate () changes the f.Size () of the file and this changes the length of the file, the function prototype func Truncate(name string, size int64) error, remember the second one is int64

import (
 "fmt"
 "os"
) func main() {
 f, _ := os.Stat("1.go")
 fmt.Println(f.Size())   //1.go 83
 err := os.Truncate("1.go", 10) 
 if err != nil {
  fmt.Println(err)
 }
 f, _ = os.Stat("1.go")
 fmt.Println(f.Size())    //1.go Now it is 10 The file also becomes package ma
}

os.Create () this function is the creation of a file, the prototype of the function is func Create(name string) (file *File, err error) input is the name string type, return is 1 File pointer and 1 error

import (
 "fmt"
 "os"
 "reflect"
) func main() {
 f, _ := os.Create("widuu_2.go")
 defer f.Close()
 fmt.Println(reflect.ValueOf(f).Type()) //*os.File
}

This function actually works like this: OpenFile(name, O_RDWR|, O_CREATE|, O_CREATE|, syscall_TRUNC, 0666) O_RDWR, O_CREATE, O_CREATE, O_CREATE, O_TRUNC, O_TRUNC, O_TRUNC, O_TRUNC, O_TRUNC, 0666) After the creation of cry ~ nothing ~~ when using the need to be careful, first judge whether the file exists ~

The prototype of the os.OpenFile function is func OpenFile(name string, flag int, perm FileMode) (file *File, err error)


import (
 "fmt"
 "os"
) func main() {
 f, _ := os.OpenFile("10.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
 defer f.Close()
 fmt.Println(f.Stat())
}

So this is Create() up here but with permission 0777 and the operations down here mostly use OpenFile()

os.Open () this function is used to open the file. The function prototype is func Open(name string) (file *File, err error). The return value is not the same as that of file *File, err error)


import (
 "fmt"
 "os"
 "reflect"
) func main() {
 f, _ := os.Open("1.go")
 defer f.Close()
}

os.Stat () this is getting the structure description of fileinfo. func Stat(name string) (fi FileInfo, err error) returns the structure of Fileinfo, which we talked about in detail earlier
How does it actually work? Because we didn't talk about syscall so we're going to talk about one, so FileInfo gets fs := at the bottom & fileStat{name: basename(name)} and then you can look at the source code

import (
 "fmt"
 "os"
) func main() {
 f, _ := os.Stat("1.go")
 fmt.Println(f.Size())
}

os.Fd () returns the handle to the file, func (file *File) Fd() uintptr returns the handle to the file, what's the handle? Handle, which is the basis of the entire windows programming. A handle is a 1-only integer used

import (
 "fmt"
 "os"
) func main() {
 f, _ := os.Open("1.go")
 fmt.Println(f.Fd()) // My platform handle is 228
}

os.Pipe () this function retrieves the read-write pointer to the function, the function prototype func Pipe() (r *File, w *File, err error)

import (
 "fmt"
 "os"
) func main() {
 r, w, _ := os.Pipe()
 fmt.Println(r, w) //&{0xc08402e120} &{0xc08402e180}
}

os.NewFile () function prototype is func NewFile(fd uintptr, name string) *File the first pass in is the handle, then the file name, this function does not actually create a file, is a new file is not saved, and then returns a pointer to the file

import (
 "fmt"
 "os"
) func main() {
 f, _ := os.Open("ini.go")
 defer f.Close()
 f1 := os.NewFile(f.Fd(), "ceshi.go") // To lose, such as ini.go The handle of
 defer f1.Close()
 fd, _ := f1.Stat()
 fmt.Println(fd.ModTime()) // Returns the ini.go Time of creation 2013-11-27 09:11:50.2793737 +0800 CST }

(f *File).Chdir() modify working directory, function prototype func (f *File) Chdir() error, this time f must be a directory, but this does not support windows

import (
 "fmt"
 "os"
) func main() {
 dir, _ := os.Getwd()
 fmt.Println(dir)
 f, _ := os.Open("views")
 err := f.Chdir()
 if err != nil {
  fmt.Println(err)
 }
 dir1, _ := os.Getwd()
 fmt.Println(dir1)
}


Related articles: