common.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package mc
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. )
  9. func httpReq(url string) []byte {
  10. resp, err := http.Get(url)
  11. if err != nil {
  12. print(err)
  13. }
  14. defer resp.Body.Close()
  15. body, err := ioutil.ReadAll(resp.Body)
  16. if err != nil {
  17. print(err)
  18. }
  19. return body
  20. }
  21. func downloadFile(filepath string, url string) error {
  22. // Get the data
  23. resp, err := http.Get(url)
  24. if err != nil {
  25. return err
  26. }
  27. defer resp.Body.Close()
  28. // Create the file
  29. out, err := os.Create(filepath)
  30. if err != nil {
  31. return err
  32. }
  33. defer out.Close()
  34. // Write the body to file
  35. _, err = io.Copy(out, resp.Body)
  36. return err
  37. }
  38. //https://opensource.com/article/18/6/copying-files-go
  39. func copy(src, dst string) (int64, error) {
  40. sourceFileStat, err := os.Stat(src)
  41. if err != nil {
  42. return 0, err
  43. }
  44. if !sourceFileStat.Mode().IsRegular() {
  45. return 0, fmt.Errorf("%s is not a regular file", src)
  46. }
  47. source, err := os.Open(src)
  48. if err != nil {
  49. return 0, err
  50. }
  51. defer source.Close()
  52. destination, err := os.Create(dst)
  53. if err != nil {
  54. return 0, err
  55. }
  56. defer destination.Close()
  57. nBytes, err := io.Copy(destination, source)
  58. return nBytes, err
  59. }