util.go 695 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. )
  9. func CheckError(err error) {
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. func Get(url string) (body string, err error) {
  15. if url == "" {
  16. err = errors.New("url is empty\n" + url)
  17. return
  18. }
  19. fmt.Println("url:", url)
  20. resp, err := http.Get(url)
  21. if err != nil || resp.StatusCode != 200 {
  22. err = errors.New("this site is not available\n" + url)
  23. return
  24. }
  25. var data []byte
  26. defer resp.Body.Close()
  27. data, err = ioutil.ReadAll(resp.Body)
  28. if err != nil {
  29. return
  30. }
  31. body = string(data)
  32. return
  33. }
  34. func GetFloat32(s string) float32 {
  35. v, err := strconv.ParseFloat(s, 32)
  36. if err != nil {
  37. v = 0
  38. }
  39. return float32(v)
  40. }