另客网go项目公用的代码库

file.go 813B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package utils
  2. import (
  3. "io"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. )
  8. // FileExists check file is exists
  9. func FileExists(path string) bool {
  10. _, err := os.Stat(path)
  11. if err != nil {
  12. if os.IsExist(err) {
  13. return true
  14. }
  15. return false
  16. }
  17. return true
  18. }
  19. // PidExists checkou process id is exists
  20. func PidExists(pid int) bool {
  21. out, _ := exec.Command("kill", "-s", "0", strconv.Itoa(pid)).CombinedOutput()
  22. if string(out) == "" {
  23. return true // pid exist
  24. }
  25. return false
  26. }
  27. // CopyFile copy file from src to dest
  28. func CopyFile(srcFile, destFile string) error {
  29. file, err := os.Open(srcFile)
  30. if err != nil {
  31. return err
  32. }
  33. defer file.Close()
  34. dest, err := os.Create(destFile)
  35. if err != nil {
  36. return err
  37. }
  38. defer dest.Close()
  39. _, err = io.Copy(dest, file)
  40. if err != nil {
  41. return err
  42. }
  43. return nil
  44. }