zoukankan      html  css  js  c++  java
  • writing files _ golang

    Writing files in Go follows similar patterns to the ones we saw earlier for reading

    package main
    
    import (
        "bufio"
        "fmt"
        "io/ioutil"
        "os"
    )
    
    func check(e error) {
        if e != nil {
            panic(e)
        }
    }
    
    func main() {
    
        d1 := []byte("hello
    go
    ")
        err := ioutil.WriteFile("/tmp/dat", d1, 0644)
        check(err)
    
        f, err := os.Create("/tmp/dat")
        check(err)
    
        defer f.Close()
    
        d2 := []byte{115, 111, 109, 101, 10}
        n2, err := f.Write(d2)
        check(err)
        fmt.Println("wrote %d bytes
    ", n2)
    
        n3, err := f.WriteString("writes
    ")
        fmt.Println("wrote %d bytes
    ", n3)
    
        f.Sync()
    
        w := bufio.NewWriter(f)
        n4, err := w.WriteString("buffered
    ")
        fmt.Println("wrote %d bytes
    ", n4)
    
        w.Flush()
    }
    wrote %d bytes
     5
    wrote %d bytes
     7
    wrote %d bytes
     9

    总结 : 

      1 : ....

  • 相关阅读:
    大数据学习操作笔记
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    jstl标签,c:foreach无效的问题
    阅读笔记
    《高效能人士的7个习惯》
  • 原文地址:https://www.cnblogs.com/jackkiexu/p/4377854.html
Copyright © 2011-2022 走看看