Go 개발 Struct 를 map 로 전환 하 는 두 가지 방식 비교

4031 단어
최근 Go 개발 을 하면 서 새로운 orm 제3자 프레임 워 크 gorose 를 접 하 게 되 었 습 니 다. 사용 하 는 과정 에서 beego 와 같은 struct 구 조 를 직접 조작 하 는 방법 이 없 었 습 니 다. 일부 API 는 map 를 통 해 데이터 베 이 스 를 조작 하 는 것 이 었 습 니 다. 그러면 struct 를 map 로 전환 해 야 합 니 다. 다음은 제 가 서로 다른 struct 를 map 로 전환 하 는 방법 두 가 지 를 시도 해 보 겠 습 니 다.
mport (
    "encoding/json"
    "fmt"
    "reflect"
    "time"
)

type Persion struct {
    Id       int
    Name     string
    Address  string
    Email    string
    School   string
    City     string
    Company  string
    Age      int
    Sex      string
    Proviece string
    Com      string
    PostTo   string
    Buys     string
    Hos      string
}

func main() {
    StructToMapViaJson()
    //StructToMapViaReflect()
}

func StructToMapViaJson() {
    m := make(map[string]interface{})
    t := time.Now()
    person := Persion{
        Id:       98439,
        Name:     "zhaondifnei",
        Address:  "   ",
        Email:    "[email protected]",
        School:   "       ",
        City:     "zhongguoguanzhou",
        Company:  "sndifneinsifnienisn",
        Age:      23,
        Sex:      "F",
        Proviece: "jianxi",
        Com:      "      ",
        PostTo:   "  XXXXXXXX",
        Buys:     "shensinfienisnfieni",
        Hos:      "zhonsndifneisnidnfie",
    }
    j, _ := json.Marshal(person)
    json.Unmarshal(j, &m)
    fmt.Println(m)
    fmt.Println(time.Now().Sub(t))
}

1. struct 를 통 해 json, json 을 map 로 전환
func StructToMapViaJson() {
    m := make(map[string]interface{})
    t := time.Now()
    person := Persion{
        Id:       98439,
        Name:     "zhaondifnei",
        Address:  "   ",
        Email:    "[email protected]",
        School:   "       ",
        City:     "zhongguoguanzhou",
        Company:  "sndifneinsifnienisn",
        Age:      23,
        Sex:      "F",
        Proviece: "jianxi",
        Com:      "      ",
        PostTo:   "  XXXXXXXX",
        Buys:     "shensinfienisnfieni",
        Hos:      "zhonsndifneisnidnfie",
    }
    j, _ := json.Marshal(person)
    json.Unmarshal(j, &m)
    fmt.Println(m)
    fmt.Printf("duration:%d", time.Now().Sub(t))
}

출력: map [Proviece: jianxi Com: 광저우 람 보 르 기니 Hos: zhonsndifneisnidnfie Name: zhaondifnei Company: sndifneinsifninisn Buys: shensinfieni 나이: 23 PostTo: 푸 른 고래 XXXXX XXXX 주소: 대사 지 학교: 광저우 제1 5 중학교 도시: zhongguoguanzhou Sex: F Id: 98439 이메일:[email protected]] duration:250467
2. 반사 형식 으로 맵 생 성
func StructToMapViaReflect() {
    m := make(map[string]interface{})
    t := time.Now()
    person := Persion{
        Id:       98439,
        Name:     "zhaondifnei",
        Address:  "   ",
        Email:    "[email protected]",
        School:   "       ",
        City:     "zhongguoguanzhou",
        Company:  "sndifneinsifnienisn",
        Age:      23,
        Sex:      "F",
        Proviece: "jianxi",
        Com:      "      ",
        PostTo:   "  XXXXXXXX",
        Buys:     "shensinfienisnfieni",
        Hos:      "zhonsndifneisnidnfie",
    }
    elem := reflect.ValueOf(&person).Elem()
    relType := elem.Type()
    for i := 0; i < relType.NumField(); i++ {
        m[relType.Field(i).Name] = elem.Field(i).Interface()
    }
    fmt.Println(m)
    fmt.Printf("duration:%d", time.Now().Sub(t))
}

출력: map [Buys: shensinfienisnfienfieni Name: zhaondifnei City: zhongguoguanzhou Sex: F Proviece: jianxi Com: 광저우 람 보 르 기니 Id: 98439 학교: 광저우 제1 5 중학교 주소: 대사 지 나이: 23 게시 물: 푸 른 고래 XXXXX XXXX 호: zhonsndifneisnidnfie 이메일:[email protected] Company:sndifneinsifnienisn] duration:104239
결론.
비 교 를 통 해 알 수 있 듯 이 반사 적 인 형식 전환 은 기본적으로 제 이 슨 형식 으로 전환 하 는 두 배 이다.

좋은 웹페이지 즐겨찾기