读写:
filePath := "/Users/zhou/go/src/zhouTest/test.txt" // 写入数据 content := []byte("hello world!") if err := ioutil.WriteFile(filePath,content,os.ModeAppend); err != nil { fmt.Println(err) } // 读取数据 res, err := ioutil.ReadFile(filePath) if err != nil { fmt.Println(err) } fmt.Printf("%s",res)
计算文件大小:
func FileSize() string { f,err := os.OpenFile("hehe.txt", os.O_RDONLY|os.O_CREATE, 0755) if err != nil { log.Println(err) } fileInfo, _ := f.Stat() size := float64(fileInfo.Size()) switch { case size < 1024: return fmt.Sprintf("%fB", size) case size < 1048576: return fmt.Sprintf("%.1fKB", size/1024) case size < 1073741824: return fmt.Sprintf("%.1fMB", size/1048576) default: return fmt.Sprintf("%.1fGB", size/1073741824) } }