misc.go 722 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. // WriteResponse writes msg to response
  7. func WriteResponse(w http.ResponseWriter, statusCode int, statusText string, msg string) {
  8. w.WriteHeader(statusCode)
  9. fmt.Fprintf(w, `{"status":"%s","message":"%s"}`, statusText, msg)
  10. }
  11. func Contains(s []string, v string) bool {
  12. for _, a := range s {
  13. if a == v {
  14. return true
  15. }
  16. }
  17. return false
  18. }
  19. // ToStrings return slice of string
  20. func ToStrings(data interface{}) []string {
  21. var values []string
  22. if vv, ok := data.([]interface{}); ok {
  23. for _, in := range vv {
  24. values = append(values, fmt.Sprint(in))
  25. }
  26. }
  27. return values
  28. }
  29. // ToString return string
  30. func ToString(data interface{}) string {
  31. return fmt.Sprint(data)
  32. }