You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

new.go 605B

12345678910111213141516171819202122232425
  1. // Copyright 2021 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package utils
  5. import "reflect"
  6. // New creates a value according type
  7. func New(tp reflect.Type, length, cap int) reflect.Value {
  8. switch tp.Kind() {
  9. case reflect.Slice:
  10. slice := reflect.MakeSlice(tp, length, cap)
  11. x := reflect.New(slice.Type())
  12. x.Elem().Set(slice)
  13. return x
  14. case reflect.Map:
  15. mp := reflect.MakeMapWithSize(tp, cap)
  16. x := reflect.New(mp.Type())
  17. x.Elem().Set(mp)
  18. return x
  19. default:
  20. return reflect.New(tp)
  21. }
  22. }