case "bindList":
type idsRequest struct {
Ids []int
}
ids := new(idsRequest)
cx.Fill(ids)
hitStorehouses := make([]module.HitStorehouse, 0)
goblet.DB.Where("status = ?",module.HitStorehouse_Status_Aliable).In("import_excel_id",ids.Ids).Find(&hitStorehouses)
arrayInterface := splitArrayInterface(100, hitStorehouses)
for index,_ := range arrayInterface {
go func(i int) {
for _, value := range arrayInterface[i] {
log.Println(value.(module.HitStorehouse).Id," ", value.(module.HitStorehouse).PhoneMd5," ", value.(module.HitStorehouse).PhoneHash," ", value.(module.HitStorehouse).ImportExcelId," ", value.(module.HitStorehouse).Status)
}
}(index)
}
func splitArrayInterface( num int64,arr interface{}) (segmens [][]interface{}) {
v := reflect.ValueOf(arr)
max := int64(v.Len())
fmt.Println("max:",max)
ret := make([]interface{}, max)
for i := 0; i < int(max); i++ {
ret[i] = v.Index(i).Interface()
}
fmt.Println("ret:", len(ret))
if max < num {
segmens = append(segmens, ret)
return
}
var step = max / num
var beg int64
var end int64
for i := int64(0); i < num || end < max; i++ {
beg = 0 + i*step
end = beg + step
if end > max {
segmens = append(segmens, ret[beg:max])
} else {
segmens = append(segmens, ret[beg:end])
}
//fmt.Printf("beg:%v,end:%v\n", beg, end)
}
return
}
参考:https://segmentfault.com/q/1010000000198391
func ToSlice(arr interface{}) []interface{} {
v := reflect.ValueOf(arr)
if v.Kind() != reflect.Slice {
panic("toslice arr not slice")
}
l := v.Len()
ret := make([]interface{}, l)
for i := 0; i < l; i++ {
ret[i] = v.Index(i).Interface()
}
return ret
}
func ToStr(i interface{}) string {
return fmt.Sprintf("%v", i)
}
ToStr(1)
ToStr(float64(1))
func Join(i []interface{}) string {
}
Join([]int{1,2}) // 报错:类型不匹配
Join(ToSlice([]int{1,2}))
网友评论