go-reflect

How to I convert reflect.New's return value back to the original type

痴心易碎 提交于 2019-12-10 22:55:33
问题 I'm using reflection in go and I noticed the oddity expressed below: package main import ( "log" "reflect" ) type Foo struct { a int b int } func main() { t := reflect.TypeOf(Foo{}) log.Println(t) // main.Foo log.Println(reflect.TypeOf(reflect.New(t))) // reflect.Value not main.Foo } How can I convert the reflect.Value back to main.Foo ? I've provided a go playground for convenience. 回答1: You use the Value.Interface method to get an interface{} , then you can use a type assertion to extract

Golang: get the type of slice

天涯浪子 提交于 2019-12-05 00:51:10
I am using reflect package to get the type of arbitrary array, but getting prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status] How do I get the type from array? I know how to get it from value. func GetTypeArray(arr []interface{}) reflect.Type { return reflect.TypeOf(arr[0]) } http://play.golang.org/p/sNw8aL0a5f Change: GetTypeArray(arr []interface{}) to: GetTypeArray(arr interface{}) By the way, []int is not an array but a slice of integers. The fact that you're indexing the slice is unsafe - if it's empty, you'll

How to check if an object has a particular method?

帅比萌擦擦* 提交于 2019-12-04 10:05:54
问题 In Go, how do you check if an object responds to a method? For example, in Objective-C this can be achieved by doing: if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists [obj methodName:42]; // call the method } 回答1: A simple option is to declare an interface with just the method you want to check for and then do a type assert against your type like; i, ok := myInstance.(InterfaceImplementingThatOneMethodIcareAbout) // inline iface delcataration example i, ok =

Selecting a function from a list of functions in Golang

强颜欢笑 提交于 2019-12-04 03:23:55
问题 Basically if I have a slice or array of any arbitrary functions, how can I select only the ones that return int, or select only the ones that take ints? I figured that I would need to use the reflect package, but just reading the docs didn't really help me figure out exactly how to do it. 回答1: This program prints the functions taking an int as parameter or returning an int : package main import ( "fmt" "reflect" ) func main() { funcs := make([]interface{}, 3, 3) // I use interface{} to allow

How to check if an object has a particular method?

别等时光非礼了梦想. 提交于 2019-12-03 05:53:53
In Go, how do you check if an object responds to a method? For example, in Objective-C this can be achieved by doing: if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists [obj methodName:42]; // call the method } A simple option is to declare an interface with just the method you want to check for and then do a type assert against your type like; i, ok := myInstance.(InterfaceImplementingThatOneMethodIcareAbout) // inline iface delcataration example i, ok = myInstance.(interface{F()}) You likely want to use the reflect package if you plan to do anything too crazy with

Selecting a function from a list of functions in Golang

ε祈祈猫儿з 提交于 2019-12-01 17:20:49
Basically if I have a slice or array of any arbitrary functions, how can I select only the ones that return int, or select only the ones that take ints? I figured that I would need to use the reflect package, but just reading the docs didn't really help me figure out exactly how to do it. This program prints the functions taking an int as parameter or returning an int : package main import ( "fmt" "reflect" ) func main() { funcs := make([]interface{}, 3, 3) // I use interface{} to allow any kind of func funcs[0] = func (a int) (int) { return a+1} // good funcs[1] = func (a string) (int) {

How to know if a variable of arbitrary type is Zero in Golang?

不羁岁月 提交于 2019-12-01 17:16:16
Because not all types are comparable, e.g. a slice. So we can't do this var v ArbitraryType v == reflect.Zero(reflect.TypeOf(v)).Interface() Edit - Solution reflect.DeepEqual var v ArbitratyType zero := reflect.Zero(reflect.TypeOf(v)).Interface() isZero := reflect.DeepEqual(v, zero) Go documentation about reflect.DeepEqual DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. Peter Noyes See this post: Golang: Reflection - How to get zero value of a field type Basically you need to have special cases

How to know if a variable of arbitrary type is Zero in Golang?

蹲街弑〆低调 提交于 2019-12-01 16:28:27
问题 Because not all types are comparable, e.g. a slice. So we can't do this var v ArbitraryType v == reflect.Zero(reflect.TypeOf(v)).Interface() Edit - Solution reflect.DeepEqual var v ArbitratyType zero := reflect.Zero(reflect.TypeOf(v)).Interface() isZero := reflect.DeepEqual(v, zero) Go documentation about reflect.DeepEqual DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. 回答1: See this post:

How to properly use .Call in reflect package

时光怂恿深爱的人放手 提交于 2019-11-30 13:54:33
Been having one last issue with my code which involves the .Call function in the reflect package. So I'm making a call such as this: params := "some map[string][]string" in := make([]reflect.Value,0) return_values := reflect.ValueOf(&controller_ref).MethodByName(action_name).Call(in) where the method I'm making the .Call to is as follows: func (c *Controller) Root(params map[string][]string) map[string] string{} What I don't quite understand is how to manipulate the "in" variable in order to properly pass the map I need to into the function. I see that the second parameter in the make() is the

Get name of function using reflection

情到浓时终转凉″ 提交于 2019-11-30 12:23:14
问题 I'm trying to use Go's reflection system to retrieve the name of a function but I get an empty string when calling the Name method on its type. Is this the expected behavior? This is a simple example of how I approach the problem: package main import "fmt" import "reflect" func main() { typ := reflect.TypeOf(main) name := typ.Name() fmt.Println("Name of function" + name) } 回答1: The solution is to use FuncForPc which returns a *Func . This returns "main.main" : package main import "fmt" import