Explain the common function usage of OS Library in go language with examples

Time:2021-6-11

(f * file). Name() is the function that returns the name of the file. Func (f * file) name() string, the prototype of the function, operates on the pointer of the file and returns a string. It seems that the underlying implementation of the method is more helpful

Copy codeThe code is as follows:
 func (f *File) Name() string { return f.name }
 import (
 “fmt”
 “os
)

func main() {
 f, _ := os.Open(“1.go“)
  FMT. Println (f.name()) / / output 1.go
}

(f * file). Read() is operated by the pointer of the function. It belongs to the method of * file. The prototype func (f * file) read (B [] byte) (n int, err error) inputs the number of bytes read, and returns the length of bytes and error information

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

func main() {
  B: = make ([] byte, 100) / / set the number of bytes read
 f, _ := os.Open(“11.go”)
 n, _ := f.Read(b)
 fmt.Println(n) 
  FMT. Println (string (B [: n]) / / why is the output n instead of entering 100 directly? The bottom layer is implemented like this
 /*
  n, e := f.read(b)
     if n < 0 {
       n = 0
     }
  if n == 0 && len(b) > 0 && e == nil {
     return 0, io.EOF
    }
   */
   // So if the byte is less than 100, read n
}

(f * file). The prototype of readat() function is func (f * file) readat (B [] byte, off Int64) (n int, error) with subscript. You can customize how much to read

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

func main() {
 f, _ := os.Open(“11.go”)
 b := make([]byte, 20)
 n, _ := f.ReadAt(b, 15)
 fmt.Println(n)
 fmt.Println(string(b[:n]))
}

(f * file). Readdir() function prototype func (f * file) readdir (n int) (FI [] FileInfo, err error). We need to open a folder, and then set the number of files in the folder to read. What we return is the FileInfo information of the file

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

func main() {
  f, err := os.Open(“src”)  // Open a directory
 if err != nil {
  fmt.Println(err)
 }
 defer f.Close()
  ff, _ := f.Readdir(10)     // Setting the number of reads < = 0 is the returned [] FileInfo after reading all the files
 for i, fi := range ff {
   fmt.Printf(“filename %d: %+v\n”, i, fi.Name())   // The name of our output file
 }
}

(f * file). Readdirnames is used to read the file name in the directory. In fact, we have implemented the function of the previous function. The prototype func (f * file) readdirnames (n int) (names [] string, error error) of the function only returns the slice of the file name [] string

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

func main() {
 f, _ := os.Open(“bin”)
 names, err := f.Readdirnames(0)
 if err != nil {
  fmt.Println(err)
 }
 for i, name := range names {
  fmt.Printf(“filename %d: %s\n”, i, name)
 }
}

The prototype of the function is func (f * file) seek (offset Int64, when int) (RET Int64, err error), where offset is the position of the file pointer, when 0 represents the position of the beginning of the relative file, and 1 represents the relative current position, 2 represents the position relative to the end of the file. RET returns the position of the current pointer

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

func main() {
 b := make([]byte, 10)
 f, _ := os.Open(“1.go”)
 defer f.Close()
  f.Seek(1, 0)     // Equivalent to start position offset 1
 n, _ := f.Read(b)
  fmt.Println(string(b[:n]))   // Original character package output ackage
}

The function prototype func (f * file) write (B [] byte) (n int, error) returns the number of bytes written in n

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

func main() {
  f, _ := os.OpenFile(“1.go”, os.O_ RDWR|os.O_ Append, 0755) / / open the file by appending, reading and writing
  n, _ := f.Write([]byte(“helloword”))                      // We write hellword
  fmt.Println(n)                                            // Print the number of bytes written
 b := make([]byte, 20)
  f.Seek(0, 0)            // The pointer returns to 0
 data, _ := f.Read(b)
  fmt.Println(string(b[:data]))        // Output the package Hello word
}

The function prototype is func (f * file) writeat (B [] byte, off Int64) (n int, err error). The return value is the same

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

func main() {
 f, _ := os.OpenFile(“1.go”, os.O_RDWR, os.ModePerm)
  f. Writeat ([] byte (“widuu”), 10) / / write at offset 10
 b := make([]byte, 20)
  d, _ := f.ReadAt(b, 10)     // Start reading at offset 10
 fmt.Println(string(b[:d])) //widuudhellowordhello
}

(f * file). Writestring() is very simple. The return value of write string function prototype func (f * file) writestring (s string) (RET, int, error) is the same

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

func main() {
 f, _ := os.OpenFile(“2.go”, os.O_RDWR, os.ModePerm)
  n, _ := f.WriteString(“hello word widuu”)  // Write string
 fmt.Println(n)
 b := make([]byte, n)
  f.Seek(0, 0)     // Be sure to set the offset address to 0, otherwise it will always be at the end of the write
 c, _ := f.Read(b)
  FMT. Println (string (B [: C]) / / returns Hello word widuu
}