mirror of
https://github.com/hanzoai/xorm.git
synced 2026-10-01 00:49:40 +00:00
26 lines
620 B
Go
26 lines
620 B
Go
// Copyright 2021 The Xorm Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package utils
|
|
|
|
import "reflect"
|
|
|
|
// New creates a value according type
|
|
func New(tp reflect.Type, length, capacity int) reflect.Value {
|
|
switch tp.Kind() {
|
|
case reflect.Slice:
|
|
slice := reflect.MakeSlice(tp, length, capacity)
|
|
x := reflect.New(slice.Type())
|
|
x.Elem().Set(slice)
|
|
return x
|
|
case reflect.Map:
|
|
mp := reflect.MakeMapWithSize(tp, capacity)
|
|
x := reflect.New(mp.Type())
|
|
x.Elem().Set(mp)
|
|
return x
|
|
default:
|
|
return reflect.New(tp)
|
|
}
|
|
}
|