Introduction to the common functions in the OS standard library of golang

Time:2021-6-12

The prototype of OS. Rename() function is func rename (old name, new name string) error. You enter the old file name, new file name, and return an error. In fact, the real implementation of this function uses syscall. Rename() and rename it by MoveFile (from * uint16, to * uint16) (error error error) = movefilew

Copy codeThe code is as follows:
 import (
 “fmt”
 “os
)

func main() {
 err := os.Rename(“1.go“, “2.go”)
 if err != nil {
   If OS. Isexist (ERR) {/ / an error to determine whether a file already exists
    FMT. Println (“file already exists”)
   os.Rename(“1.go”, “widuu_1.go”)
  }
 }
}

OS. Samefile() is used to check whether the file information is the same. The so-called file information refers to OS. Stat(). The prototype of the function is func samefile (FI1, fi2 FileInfo) bool
for instance

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

func main() {
 f1, _ := os.Stat(“1.go”)
 f2, _ := os.Stat(“21.go”)
 if os.SameFile(f1, f2) {
   FMT. Println (“the two files are the same”)
  return
 }
  FMT. Println (“two files are different”)
}

OS. Setenv() is a very simple function to set environment variables. The prototype func setenv (key, value string) error is used to input the corresponding key value string and return the error information

Copy codeThe code is as follows:
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 D / golang
}

OS. Symlink() for this function, I can only say that it does not support Windows platform. Create a soft connection func symlink (old name, new name string) error

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

func main() {
  Err: = OS. Symlink (“1. Go”, “21. Go”) / / does not support Windows platform, only supports Linux and UNIX
 fmt.Println(err)
}

OS. Tempdir() this function is very simple, return to your local system temp directory, function prototype func tempdir() string, hehe, make a comparison, don’t mess up

Copy codeThe code is as follows:
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 returns the system temp
 temp := os.TempDir()
  FMT. Println (Temp) / / for windows, C:: (users) admin ~ 1 (appdata) local (Temp)
}

OS. Truncate() changes the file’s f.size(). This changes the length of the file’s content. Func truncate (name string, size Int64) error is the function prototype. Remember, the second one is Int64

Copy codeThe code is as follows:
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 is now 10, and the file has become package ma
}

OS. Create() creates a file. The prototype of the function is func create (name string) (file * file, error). The input is a name string type, and the return is a file pointer and an error

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
 “reflect”
)

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

The principle of this function is openFile (name, O_ RDWR|O_ CREATE|O_ TRUNC, 0666) O_ Rdwr, that is to say, with read-write permission, O_ Create then the file exists, ignore, do not exist, create it, O_ TRUNC file exists, and the intercept length is 0, which explains why we have this file, I wipe it, and cry after creating it ~ there is nothing ~ ~ when using it, we need to be careful to judge whether the file exists first~

The prototype of OS. OpenFile function is func openFile (name, string, flag, int, perm, FileMode) (file * file, error error). We need to specify the file permission and open method, which is what we used above

Copy codeThe code is as follows:
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())
}

This is the create() above, but the permission is 0777 and most of the operations below use openFile ()

OS. Open() is used to open a file. The prototype of the function is func open (name string) (file * file, error). The return value is not the same. Its principle is openFile (name, O_ Rdonly, 0) is opened in read file mode

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
 “reflect”
)

func main() {
 f, _ := os.Open(“1.go”)
 defer f.Close()
}

OS. Stat() is the structure description of getting FileInfo. Func stat (name string) (FI FileInfo, error error) returns the structure of FileInfo, which we’ll talk about in detail
In fact, how is it realized? Because we didn’t talk about syscall, we’ll talk about one, such as FileInfo underlying access FS: = & filestat {Name: basename (name)} and then the following logic. You can see the source code

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

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

OS. Fd() returns a handle to a file. The function prototype is func (file * file) fd() uintptr. The function uintptr (file. FD) returns a handle to a file. What is the handle? Handle is the basis of the whole windows programming. A handle is a unique integer used

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

func main() {
 f, _ := os.Open(“1.go”)
  FMT. Println (f.fd()) / / my platform handle is 228
}

OS. Pipe() is the read-write pointer of the function obtained by this function. The function prototype func pipe() (R * file, w * file, error)

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

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

The prototype of OS. Newfile() function is func newfile (FD uintptr, name string) * file. The first pass in is the handle, and then the file name. This function does not really create a file, but creates a new file without saving, and then returns the file pointer

Copy codeThe code is as follows:
import (
 “fmt”
 “os”
)

func main() {
 f, _ := os.Open(“ini.go”)
 defer f.Close()
  F1: = OS. Newfile (f.fd(), “ceshi. Go”) / / enter the handle of ini.go
 defer f1.Close()
 fd, _ := f1.Stat()
  fmt.Println(fd.ModTime())  // The creation time of ini.go is 2013-11-27 09:11:50.2793737 + 0800 CST

}

(f * file). Chdir() modifies the working directory. The function prototype func (f * file) chdir() error. At this time, f must be a directory, but this does not support windows

Copy codeThe code is as follows:
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)
}