util.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import "reflect"
  3. import "github.com/jinzhu/gorm"
  4. const DateTimeFormat = "2006-01-02 15:04:05"
  5. func DatasToMap(datas []string) map[string]string {
  6. m := make(map[string]string, len(datas)/2)
  7. for i, data := range datas {
  8. if i%2 == 0 {
  9. m[data] = ""
  10. } else {
  11. key := datas[i-1]
  12. m[key] = data
  13. }
  14. }
  15. return m
  16. }
  17. // 根据参数名获取对应参数的值
  18. func GetValue(data interface{}, name string) interface{} {
  19. value := reflect.ValueOf(data)
  20. for i := 0; i < value.NumField(); i++ {
  21. em := value.Field(i)
  22. if em.Kind() != reflect.Struct {
  23. continue
  24. }
  25. for j := 0; j < em.NumField(); j++ {
  26. varName := em.Type().Field(j).Tag.Get("json")
  27. if varName != name {
  28. continue
  29. }
  30. return em.Field(j).Interface()
  31. }
  32. }
  33. return nil
  34. }
  35. func GetLinkSeeConnection() (db *gorm.DB, err error) {
  36. //此为公网连接,供测试阶段使用,生产环境需要替换成内网地址连接:
  37. //内网地址:rm-bp1rxj4r7a204ujhn.mysql.rds.aliyuncs.com
  38. //外网地址:rm-bp1rxj4r7a204ujhnio.mysql.rds.aliyuncs.com
  39. db, err = gorm.Open("mysql", "common_user:Meiyoumima1234@tcp(rm-bp1rxj4r7a204ujhn.mysql.rds.aliyuncs.com:3306)/linksee?charset=utf8&parseTime=True&loc=Local")
  40. if err != nil {
  41. //LZPrint("db.conn err is:", err.Error())
  42. }
  43. return db, err
  44. }