一、 文件是存儲在外部介質上的數據集合。blog
A. 文件分類:文本文件和二進制文件ip
B. 文件存取方式:隨機存取和順序存放get
二、文件打開input
package main import ( "bufio" "fmt" "io" "os" ) func main() { //只讀的方式打開 inputFile, err := os.Open("input.dat") if err != nil { fmt.Printf("open file err:%v\n", err) return } defer inputFile.Close() }
三、文件讀取, file.Read和file.ReadAt。讀到文件末尾返回:io.EOF權限控制
package main import ( "bufio" "fmt" "io" "os" ) func main() { //只讀的方式打開 inputFile, err := os.Open("input.dat") if err != nil { fmt.Printf("open file err:%v\n", err) return } var buf[128]byte inputFile.Read(buf[:]) defer inputFile.Close() }
四、 bufio原理string
五、bufio讀取文件it
package main import ( "bufio" "fmt" "io" "os" ) func main() { inputFile, err := os.Open("input.dat") if err != nil { fmt.Printf("open file err:%v\n", err) return } defer inputFile.Close() inputReader := bufio.NewReader(inputFile) for { inputString, readerError := inputReader.ReadString('\n') if readerError == io.EOF { return } fmt.Printf("The input was: %s", inputString) } }
六、讀取整個文件示例io
package main import ( "fmt" "io/ioutil" "os" ) func main() { inputFile := "products.txt" outputFile := "products_copy.txt" buf, err := ioutil.ReadFile(inputFile) if err != nil { fmt.Fprintf(os.Stderr, "File Error: %s\n", err) return } fmt.Printf("%s\n", string(buf)) }
一、讀取壓縮文件class
package main import ( "bufio" "compress/gzip" "fmt" "os" ) func main() { fName := "MyFile.gz" var r *bufio.Reader fi, err := os.Open(fName) if err != nil { fmt.Fprintf(os.Stderr, "%v, Can’t open %s: error: %s\n", os.Args[0], fName, err) os.Exit(1) } fz, err := gzip.NewReader(fi) if err != nil { fmt.Fprintf(os.Stderr, "open gzip failed, err: %v\n", err) return } r = bufio.NewReader(fz) for { line, err := r.ReadString('\n') if err != nil { fmt.Println("Done reading file") os.Exit(0) } fmt.Println(line) } }
一、文件寫入import
文件讀寫
os.OpenFile(「output.dat」, os.O_WRONLY|os.O_CREATE, 0666)
1. os.O_WRONLY:只寫
第二個參數:文件打開模式
:
r ——> 004
w——> 002
x——> 001
第三個參數:權限控制:
2. os.O_CREATE:建立文件
3. os.O_RDONLY:只讀
4. os.O_RDWR:讀寫
5. os.O_TRUNC :清空
6. os. O_APPEND:追加
二、文件寫入示例
file.Write()
file.WriteAt()
file.WriteString()
package main import ( "bufio" "fmt" "os" ) func main() { outputFile, outputError := os.OpenFile("output.dat", os.O_WRONLY|os.O_CREATE, 0666) if outputError != nil { fmt.Printf("An error occurred with file creation\n") return } str := 「hello world」 outputFile.Write([]byte(str)) defer outputFile.Close() }
三、 文件寫入示例
package main import ( "bufio" "fmt" "os" ) func main() { outputFile, outputError := os.OpenFile("output.dat", os.O_WRONLY|os.O_CREATE, 0666) if outputError != nil { fmt.Printf("An error occurred with file creation\n") return } defer outputFile.Close() outputWriter := bufio.NewWriter(outputFile) outputString := "hello world!\n" for i := 0; i < 10; i++ { outputWriter.WriteString(outputString) } outputWriter.Flush() }
四、寫入整個文件示例
package main import ( "fmt" "io/ioutil" "os" ) func main() { inputFile := "products.txt" outputFile := "products_copy.txt" buf, err := ioutil.ReadFile(inputFile) if err != nil { fmt.Fprintf(os.Stderr, "File Error: %s\n", err) return } fmt.Printf("%s\n", string(buf)) err = ioutil.WriteFile(outputFile, buf, 0x644) if err != nil { panic(err.Error()) } }
五、拷貝文件
package main import ( "fmt" "io" "os" ) func main() { CopyFile("target.txt", "source.txt") fmt.Println("Copy done!") } func CopyFile(dstName, srcName string) (written int64, err error) { src, err := os.Open(srcName) if err != nil { return } defer src.Close() dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { return } defer dst.Close() return io.Copy(dst, src) }
六、cat命令實現
package main import ( "bufio" "flag" "fmt" "io" "os" ) func cat(r *bufio.Reader) { for { buf, err := r.ReadBytes('\n') if err == io.EOF { break } fmt.Fprintf(os.Stdout, "%s", buf) return } } func main() { flag.Parse() if flag.NArg() == 0 { cat(bufio.NewReader(os.Stdin)) } for i := 0; i < flag.NArg(); i++ { f, err := os.Open(flag.Arg(i)) if err != nil { fmt.Fprintf(os.Stderr, "%s:error reading from %s: %s\n", os.Args[0], flag.Arg(i), err.Error()) continue } cat(bufio.NewReader(f)) } }
一、defer原理分析
二、defer案例
package main import ( "fmt" ) func funcA() int { x := 5 defer func() { x += 1 }() return x } func main() { fmt.Println(funcA()) }
三、defer案列2
package main import "fmt" func funcB() (x int) { defer func() { x += 1 }() return 5 } func main() { fmt.Println(funcB()) }
四、defer案例3
package main import "fmt" func funcC() (y int) { x := 5 defer func() { x += 1 }() return x } func main() { fmt.Println(funcC()) }
五、defer案例4
package main import "fmt" func funcD() (x int) { defer func(x int) { x += 1 }(x) return 5 } func main() { fmt.Println(funcD()) }