1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| 大专栏 Golang进阶实战之IO操作"code">func () { file, err := os.Open("aaa.txt") if err != nil { return } defer file.Close()
bs := make([]byte, 4, 4)
n := -1 for { n, err = file.Read(bs) if n == 0 || err == io.EOF { break } fmt.Println(string(bs[:n])) }
file2, err2 := os.OpenFile("bbb.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm) if err2 != nil { return } defer file2.Close() bs2 := []byte{44,55,66} n2, err4 := file2.Write(bs2) if err4 != nil { return } fmt.Println(n2) }
|