-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
48 lines (43 loc) · 1.01 KB
/
instance.go
File metadata and controls
48 lines (43 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package goreflect
import (
"reflect"
)
// GetTypeOf returns the type of the given bodyType
//
// Parameters:
//
// - bodyType: The body type to get the type of
//
// Returns:
//
// - reflect.Type: The type of the given bodyType
func GetTypeOf(bodyType any) reflect.Type {
return reflect.TypeOf(bodyType)
}
// NewInstance creates a new instance of the given type
//
// Parameters:
//
// - bodyType: The body type to create a new instance of
//
// Returns:
//
// - any: A pointer to a new instance of the given type
func NewInstance(bodyType any) any {
// Get the reflect.Type of the bodyType
t := reflect.TypeOf(bodyType)
// Create a new instance of the type and return a pointer to it
return reflect.New(t).Interface()
}
// NewInstanceFromType creates a new instance of the given type
//
// Parameters:
//
// - t: The reflect.Type to create a new instance of
//
// Returns:
//
// - any: A pointer to a new instance of the given type
func NewInstanceFromType(t reflect.Type) any {
return reflect.New(t).Interface()
}