代码拉取完成,页面将自动刷新
// Copyright 2013 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package util
import (
"fmt"
"reflect"
"strings"
)
func Struct2Map(dest map[string]interface{}, src interface{}) error {
if dest == nil {
return fmt.Errorf("Struct2Map(dest is %v)", dest)
}
srcType := reflect.TypeOf(src)
srcValue := reflect.Indirect(reflect.ValueOf(src))
if srcValue.Kind() != reflect.Struct {
return fmt.Errorf("Struct2Map(non-struct %s)", srcType)
}
srcType = srcValue.Type()
fieldNum := srcType.NumField()
for i := 0; i < fieldNum; i++ {
// struct 字段的反射类型(StructField)
fieldType := srcType.Field(i)
// 非导出字段不处理
if fieldType.PkgPath != "" {
continue
}
tag := fieldType.Tag.Get("json")
fieldValue := srcValue.Field(i)
// json 有 key,omitempty 的情况
tag = strings.Split(tag, ",")[0]
if tag == "" {
tag = UnderscoreName(fieldType.Name)
}
dest[tag] = fieldValue.Interface()
}
return nil
}
// model中类型提取其中的 idField(int 类型) 属性组成 slice 返回
func Models2Intslice(models interface{}, idField string) []int {
if models == nil {
return []int{}
}
// 类型检查
modelsValue := reflect.ValueOf(models)
if modelsValue.Kind() != reflect.Slice {
return []int{}
}
var modelValue reflect.Value
length := modelsValue.Len()
ids := make([]int, 0, length)
for i := 0; i < length; i++ {
modelValue = reflect.Indirect(modelsValue.Index(i))
if modelValue.Kind() != reflect.Struct {
continue
}
val := modelValue.FieldByName(idField)
if val.Kind() != reflect.Int {
continue
}
ids = append(ids, int(val.Int()))
}
return ids
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。