package main
import (
"fmt"
"io"
"os"
)
func openCloseFile() {
f, _ := os.Open("test2.txt")
fmt.Printf("f.Name(): %v\n", f.Name())
f2, _ := os.OpenFile("a1.txt", os.O_RDWR|os.O_CREATE, 0755)
fmt.Printf("f2.Name(): %v\n", f2.Name())
err := f.Close()
fmt.Printf("err: %v\n", err)
err2 := f2.Close()
fmt.Printf("err2: %v\n", err2)
}
func createFile() {
f, _ := os.Create("a2.txt")
fmt.Printf("f.Name(): %v\n", f.Name())
f2, _ := os.CreateTemp("", "temp")
fmt.Printf("f2.Name(): %v\n", f2.Name())
}
func readOps() {
f, _ := os.Open("a1.txt")
for {
buf := make([]byte, 6)
n, err := f.Read(buf)
fmt.Printf("string(buf): %v\n", string(buf))
fmt.Printf("n: %v\n", n)
if err == io.EOF {
break
}
}
f.Close()
}
func readOps1() {
buf := make([]byte, 10)
f2, _ := os.Open("a1.txt")
n2, _ := f2.ReadAt(buf, 5)
fmt.Printf("n2: %v\n", n2)
fmt.Printf("string(buf): %v\n", string(buf))
f2.Close()
}
func readOps2() {
f, _ := os.Open("a1.txt")
f.Seek(3, 0)
b := make([]byte, 10)
n, _ := f.Read(b)
fmt.Printf("n: %v\n", n)
fmt.Printf("string(b): %v\n", string(b))
f.Close()
}
func readOps3() {
de, _ := os.ReadDir("a")
for _, v := range de {
fmt.Printf("v.IsDir(): %v\n", v.IsDir())
fmt.Printf("v.Name(): %v\n", v.Name())
}
}
func main() {
}
|