file.go 696 B

123456789101112131415161718192021222324252627282930
  1. package file
  2. import (
  3. "os"
  4. "path/filepath"
  5. cerror "github.com/cherry-game/cherry/error"
  6. cfile "github.com/cherry-game/cherry/extend/file"
  7. )
  8. // 创建目录
  9. func CreateDir(filePath string) error {
  10. if !cfile.IsDir(filePath) {
  11. if err := os.MkdirAll(filePath, 0755); err != nil {
  12. return cerror.Errorf("Create dir error. filePath = %s, err = %v", filePath, err)
  13. }
  14. }
  15. return nil
  16. }
  17. // 写文件
  18. func Write(filePath string, fileName string, data []byte) error {
  19. fullFilePath := filepath.Join(filePath, fileName)
  20. if err := os.WriteFile(fullFilePath, data, 0644); err != nil {
  21. return cerror.Errorf("Write file error. fullFilePath = %s, err = %v", fullFilePath, err)
  22. }
  23. return nil
  24. }