偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Go開發(fā)中結構體 model、dto 、time格式問題

開發(fā) 前端
model層不允許使用 json, dto層又重復造輪子,一個表的字段可能20個左右,那么賦值語句難受死了。其次就是json直接解析,model層的time.Time,完蛋格式不對,返回的數(shù)據(jù)不對。

1、背景

model層不允許使用 json, dto層又重復造輪子,一個表的字段可能20個左右,那么賦值語句難受死了。

[[441337]]

其次就是json直接解析,model層的time.Time,完蛋格式不對,返回的數(shù)據(jù)不對。

比如

 

  1.     "user_name""xiaoli"
  2.     "create_time""2020-06-05T13:53:06.293614+08:00" 

這種情況,無法解決,就需要必須重寫一個dto。

那么如何解決這個問題呢,本人思考了一段時間,最終使用Map來解決。

2、解決問題

1、反射

那么反射會遇到,各種奇葩的書寫方式,有些人什么都出傳入指針,有些人各種interface{} 隱藏轉換,反正就是太過于差異化。

所以就是需要解決,如何準確的拿到Value對象,下面是我寫的一個工具類

 

  1. func GetRealValue(value reflect.Value) reflect.Value { 
  2.     kind := value.Kind() 
  3.     if kind == reflect.Ptr { 
  4.         return GetRealValue(value.Elem()) 
  5.     } 
  6.     if kind == reflect.Interface { 
  7.         // eg:var s2 interface{} 
  8.         //  s2 = User{} 
  9.         //  fmt.Println(reflect.ValueOf(&s2).Elem().Kind())// interface 
  10.         // 所以這里需要將它轉換 
  11.         if value.CanInterface() { 
  12.             return GetRealValue(reflect.ValueOf(value.Interface())) 
  13.         } 
  14.         return GetRealValue(value.Elem()) 
  15.     } 
  16.     return value 

解決這個問題,開干

2、下劃線命名法

下劃線如何解決,結構體的字段屬于駝峰命名法,怎么解決呢?

寫了一個簡單的工具類

問題:

  • 如果是ID,連續(xù)大寫,輸出i_d
  • 因為數(shù)組到切片需要拷貝一次,所以可以利用unsafe解決,因為字符串底層就是切片,但是不安全

 

  1. func CamelCase(s string) string { 
  2.     if s == "" { 
  3.         return "" 
  4.     } 
  5.     t := make([]byte, 0, 32) 
  6.     i := 0 
  7.     for ; i < len(s); i++ { 
  8.         c := s[i] 
  9.         if isASCIIDigit(c) { 
  10.             t = append(t, c) 
  11.             continue 
  12.         } 
  13.         if isASCIIUpper(c) { 
  14.             c ^= ' ' 
  15.         } 
  16.         t = append(t, c) 
  17.         for i+1 < len(s) && isASCIIUpper(s[i+1]) { 
  18.             i++ 
  19.             t = append(t, '_', s[i]+32) 
  20.         } 
  21.     } 
  22.     //return *(*string)(unsafe.Pointer(&t)) 
  23.     return string(t) 
  24. func isASCIIUpper(c byte) bool { 
  25.     return 'A' <= c && c <= 'Z' 
  26.  
  27. func isASCIIDigit(c byte) bool { 
  28.     return '0' <= c && c <= '9' 

3、開干

  • 解決time的問題
  • 反射、下劃線命名法

 

  1. func ToStdMap(bean interface{}) map[string]interface{} { 
  2.     _value := GetRealValue(reflect.ValueOf(bean)) 
  3.     if _value.Kind() != reflect.Struct { 
  4.         panic("the bean mush struct"
  5.     } 
  6.     _type := _value.Type() 
  7.     fieldNum := _value.NumField() 
  8.     _map := make(map[string]interface{}, fieldNum) 
  9.     for x := 0; x < fieldNum; x++ { 
  10.         field := _type.Field(x) 
  11.         value := GetRealValue(_value.Field(x)) 
  12.         if value.CanInterface() { 
  13.             realValue := value.Interface() 
  14.             switch realValue.(type) { 
  15.             case time.Time
  16.                 _map[CamelCase(field.Name)] = times.FormatStdTime(realValue.(time.Time)) 
  17.             default
  18.                 _map[CamelCase(field.Name)] = realValue 
  19.             } 
  20.         } 
  21.     } 
  22.     return _map 

4、測試

 

  1. func TestObjToMap(t *testing.T) { 
  2.     users := Users{ 
  3.         UserName: "xiaoli"
  4.     } 
  5.     now := time.Now() 
  6.     users.CreateTime = &now 
  7.     stdMap := ToStdMap(users) 
  8.     bytes, err := json.Marshal(stdMap) 
  9.     if err != nil { 
  10.         t.Fatal(err) 
  11.     } 
  12.     fmt.Printf("%s\n", bytes) 

輸出結果:

完美,美中不足是需要使用likedMap,由于Golang源碼包沒有,所以,注定亂序

  1. {"create_time":"2020-06-05 14:05:31","user_name":"xiaoli"

 

責任編輯:未麗燕 來源: 今日頭條
相關推薦

2020-12-02 09:10:22

Go結構數(shù)據(jù)類型

2023-11-21 08:03:43

語言架構偏移量

2021-12-20 07:59:07

Go語言結構體

2021-11-02 12:19:18

Go函數(shù)結構

2021-04-20 09:00:48

Go 語言結構體type

2023-07-29 15:03:29

2020-11-23 08:54:14

Go語言結構體

2020-11-30 06:17:03

Go語言

2020-11-26 06:40:24

Go語言基礎

2020-12-02 08:45:36

Go語言

2021-11-02 14:54:41

Go結構體標簽

2024-10-16 09:57:52

空結構體map屬性

2025-03-03 00:05:00

GoTimer調(diào)度器

2021-11-15 06:56:46

Go語言Tag

2025-08-29 01:45:00

Go語言函數(shù)

2021-02-06 18:19:54

TimeGo語言

2025-07-29 10:00:00

指針開發(fā)Go

2025-06-04 04:10:00

HappensGo內(nèi)存

2025-06-12 00:21:27

2023-08-14 08:51:50

項目接口結構
點贊
收藏

51CTO技術棧公眾號