自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go開發(fā)中結(jié)構(gòu)體 model、dto 、time格式問題

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

1、背景

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

[[441337]]

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

比如

 

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

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

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

2、解決問題

1、反射

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

所以就是需要解決,如何準(zhǔn)確的拿到Value對(duì)象,下面是我寫的一個(gè)工具類

 

  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.         // 所以這里需要將它轉(zhuǎn)換 
  11.         if value.CanInterface() { 
  12.             return GetRealValue(reflect.ValueOf(value.Interface())) 
  13.         } 
  14.         return GetRealValue(value.Elem()) 
  15.     } 
  16.     return value 

解決這個(gè)問題,開干

2、下劃線命名法

下劃線如何解決,結(jié)構(gòu)體的字段屬于駝峰命名法,怎么解決呢?

寫了一個(gè)簡(jiǎn)單的工具類

問題:

  • 如果是ID,連續(xù)大寫,輸出i_d
  • 因?yàn)閿?shù)組到切片需要拷貝一次,所以可以利用unsafe解決,因?yàn)樽址讓泳褪乔衅?,但是不安?/li>

 

  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、測(cè)試

 

  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) 

輸出結(jié)果:

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

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

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2020-12-02 09:10:22

Go結(jié)構(gòu)數(shù)據(jù)類型

2023-11-21 08:03:43

語言架構(gòu)偏移量

2021-12-20 07:59:07

Go語言結(jié)構(gòu)體

2023-07-29 15:03:29

2021-11-02 12:19:18

Go函數(shù)結(jié)構(gòu)

2021-04-20 09:00:48

Go 語言結(jié)構(gòu)體type

2020-11-30 06:17:03

Go語言

2020-11-26 06:40:24

Go語言基礎(chǔ)

2020-12-02 08:45:36

Go語言

2020-11-23 08:54:14

Go語言結(jié)構(gòu)體

2021-11-02 14:54:41

Go結(jié)構(gòu)體標(biāo)簽

2024-10-16 09:57:52

空結(jié)構(gòu)體map屬性

2021-11-15 06:56:46

Go語言Tag

2025-03-03 00:05:00

GoTimer調(diào)度器

2021-02-06 18:19:54

TimeGo語言

2023-08-14 08:51:50

項(xiàng)目接口結(jié)構(gòu)

2022-09-18 23:09:13

Go語言標(biāo)準(zhǔn)庫

2011-04-11 13:00:08

C++結(jié)構(gòu)體枚舉

2020-12-20 09:59:13

Go語言基礎(chǔ)技術(shù)

2023-03-07 10:32:34

Go語言結(jié)構(gòu)體
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)