Examples illustrate the common use of the Go library functions in the Go language
- 2020-05-30 20:22:00
- OfStack
(f *File).Name() this function is to return the name of the file, function prototype func (f *File) Name() string to the file pointer operation, return a string, feel the chicken helper method underlying implementation
func (f *File) Name() string { return f.name }
import (
"fmt"
"os"
)
func main() {
f, _ := os.Open("1.go")
fmt.Println(f.Name()) // The output 1.go
}
(f *File).Read() this is the function pointer to operate, belongs to method *FIlE, function prototype func (f *File) Read(b []byte) (n int, err error) input the number of bytes read, return the length of the bytes and error information
import (
"fmt"
"os"
)
func main() {
b := make([]byte, 100) // Sets the number of bytes read
f, _ := os.Open("11.go")
n, _ := f.Read(b)
fmt.Println(n)
fmt.Println(string(b[:n])) // output Why is it n Instead of typing directly 100 ? The underlying implementation is this way
/*
n, e := f.read(b)
if n < 0 {
n = 0
}
if n == 0 && len(b) > 0 && e == nil {
return 0, io.EOF
}
*/
// So not enough bytes 100 Just read n
}
The prototype of this function is func (f *File) ReadAt(b []byte, off int64) (n int, err error) with the subscript added, you can customize how much you read
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 want to open a folder, and then set the number of folder files to read, and return the fileinfo information of the file
import (
"fmt"
"os"
)
func main() {
f, err := os.Open("src") // Open the 1 A directory
if err != nil {
fmt.Println(err)
}
defer f.Close()
ff, _ := f.Readdir(10) // Sets the number of reads <=0 Is to read all the files The returned []fileinfo
for i, fi := range ff {
fmt.Printf("filename %d: %+v\n", i, fi.Name()) // We output the name of the file
}
}
(f *File).Readdirnames is a function that reads the file name in the directory. In fact, we have already realized the function function of the last function, the prototype of the function func (f *File) Readdirnames(n int) (names []string, err error)
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)
}
}
f *File).Seek() is the address of the offset pointer. The prototype of the function is func (f *File). Seek(offset int64, whence int). 2 represents the position relative to the end of the file. ret returns the current position of the pointer
import (
"fmt"
"os"
)
func main() {
b := make([]byte, 10)
f, _ := os.Open("1.go")
defer f.Close()
f.Seek(1, 0) // It's the same thing as the starting offset 1
n, _ := f.Read(b)
fmt.Println(string(b[:n])) // The original character package The output ackage
}
The function prototype func (f *File) Write(b []byte) (n int, err error) returns the number of bytes written by n
import (
"fmt"
"os"
)
func main() {
f, _ := os.OpenFile("1.go", os.O_RDWR|os.O_APPEND, 0755) // Open the file by appending and reading and writing
n, _ := f.Write([]byte("helloword")) // We write hellword
fmt.Println(n) // The number of bytes printed to write
b := make([]byte, 20)
f.Seek(0, 0) // Pointer back to 0
data, _ := f.Read(b)
fmt.Println(string(b[:data])) // The output packagehelloword
}
The function prototype is func (f *File) WriteAt(b []byte, off int64) (n int, err error). The return value is the same
import (
"fmt"
"os"
)
func main() {
f, _ := os.OpenFile("1.go", os.O_RDWR, os.ModePerm)
f.WriteAt([]byte("widuu"), 10) // In the offset 10 Where to write
b := make([]byte, 20)
d, _ := f.ReadAt(b, 10) // The offset 10 The place to start reading
fmt.Println(string(b[:d])) //widuudhellowordhello
}
func (f *File) WriteString(s string) (ret int, err error
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) //1 You must return the offset address 0 Otherwise, 1 Right at the end of the write
c, _ := f.Read(b)
fmt.Println(string(b[:c])) // return hello word widuu
}