util.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. )
  10. func CheckError(err error) (r bool) {
  11. r = true
  12. if err != nil {
  13. // panic(err)
  14. r = false
  15. }
  16. return
  17. }
  18. func Get(url string) (body string, err error) {
  19. if url == "" {
  20. err = errors.New("url is empty\n" + url)
  21. return
  22. }
  23. fmt.Println("url:", url)
  24. resp, err := http.Get(url)
  25. // if err != nil || resp.StatusCode != 200 {
  26. // err = errors.New("this site is not available\n" + url)
  27. // return
  28. // }
  29. var data []byte
  30. defer resp.Body.Close()
  31. data, err = ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. return
  34. }
  35. body = string(data)
  36. return
  37. }
  38. func GetFloat32(s string) float32 {
  39. v, err := strconv.ParseFloat(s, 32)
  40. if err != nil {
  41. v = 0
  42. }
  43. return float32(v)
  44. }
  45. func Unique(intSlice []string) []string {
  46. keys := make(map[string]bool)
  47. list := []string{}
  48. for _, entry := range intSlice {
  49. if _, value := keys[entry]; !value {
  50. keys[entry] = true
  51. list = append(list, entry)
  52. }
  53. }
  54. return list
  55. }
  56. func ChunkSlice(slice []string, chunkSize int) [][]string {
  57. var chunks [][]string
  58. for i := 0; i < len(slice); i += chunkSize {
  59. end := i + chunkSize
  60. // necessary check to avoid slicing beyond
  61. // slice capacity
  62. if end > len(slice) {
  63. end = len(slice)
  64. }
  65. chunks = append(chunks, slice[i:end])
  66. }
  67. return chunks
  68. }
  69. func Contains(s []string, str string) bool {
  70. for _, v := range s {
  71. if v == str && v != "" {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. func GetPrice(str string) float32 {
  78. idx := strings.Index(str, "원")
  79. if idx > 0 {
  80. str = str[:idx]
  81. }
  82. str = strings.Replace(str, ",", "", -1)
  83. str = strings.TrimSpace(str)
  84. return GetFloat32(str)
  85. }