go-reflect

Checking the equality of two slices

落花浮王杯 提交于 2019-11-30 10:11:39
问题 How can I check if two slices are equal? 回答1: You need to loop over each of the elements in the slice and test. Equality for slices is not defined. However, there is a bytes.Equal function if you are comparing values of type []byte . func testEq(a, b []Type) bool { // If one is nil, the other must also be nil. if (a == nil) != (b == nil) { return false; } if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } 回答2: You should use reflect

In golang, is it possible to get reflect.Type from the type itself, from name as string?

半城伤御伤魂 提交于 2019-11-30 02:52:56
type t1 struct { i int; s string } var v1 reflect.Type = /* how to set to t1's reflect.Type? */ is it possible to get the reflect.Type of t1 without having to instantiate it? is it possible to get the reflect.Type of t1 from having its name "t1" as a string? Kyle C On 1, yes, kinda: var v1 reflect.Type = reflect.TypeOf((*t1)(nil)).Elem() fmt.Println(v1) // prints "main.t1" No instantiation needed. However, Go doesn't have type literals, which is I think what you're asking for. To get the runtime value of a type, you need to have a value of some sort. If you don't want to or can't create the

Get name of function using reflection in Golang

元气小坏坏 提交于 2019-11-30 02:25:29
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) } The solution is to use FuncForPc which returns a *Func . This returns "main.main" : package main import "fmt" import "reflect" import "runtime" func main() { name := runtime.FuncForPC(reflect.ValueOf(main).Pointer()).Name()

Why golang reflect.MakeSlice returns un-addressable Value

╄→гoц情女王★ 提交于 2019-11-29 22:25:15
check the Snippet below: http://play.golang.org/p/xusdITxgT- Why is this happening? Because one of my argument must be a slice address. Maybe I did not made it clear for everyone. collection.Find(bson.M{}).All(&result) The above code is why I need a slice address. the result variable here is what I need. Now usually I can do this result := make([]SomeStruct, 10, 10) But now the SomeStruct is dynamic and I need to create the slice by using reflect.MakeSlice, So result := reflect.MakeSlice(reflect.SliceOf(SomeType)) And it errors on : result must be a slice address. How to get a pointer to a

How to properly use .Call in reflect package

别来无恙 提交于 2019-11-29 19:45:39
问题 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

Checking the equality of two slices

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 18:50:45
How can I check if two slices are equal? You need to loop over each of the elements in the slice and test. Equality for slices is not defined. However, there is a bytes.Equal function if you are comparing values of type []byte . func testEq(a, b []Type) bool { // If one is nil, the other must also be nil. if (a == nil) != (b == nil) { return false; } if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } Victor Deryagin You should use reflect.DeepEqual() DeepEqual is a recursive relaxation of Go's == operator. DeepEqual reports whether x and y

In golang, is it possible to get reflect.Type from the type itself, from name as string?

断了今生、忘了曾经 提交于 2019-11-28 23:46:49
问题 type t1 struct { i int; s string } var v1 reflect.Type = /* how to set to t1's reflect.Type? */ is it possible to get the reflect.Type of t1 without having to instantiate it? is it possible to get the reflect.Type of t1 from having its name "t1" as a string? 回答1: On 1, yes, kinda: var v1 reflect.Type = reflect.TypeOf((*t1)(nil)).Elem() fmt.Println(v1) // prints "main.t1" No instantiation needed. However, Go doesn't have type literals, which is I think what you're asking for. To get the

Dynamically call method on interface{} regardless of receiver type

℡╲_俬逩灬. 提交于 2019-11-28 17:38:58
I'm working on a templating system written in Go, which means it requires liberal use of the reflect package. In this specific circumstance I need to be able to dynamically call a method on an interface{} . The oddity is that my reflection logic works fine as long as my data is of a known type, but not if the data is of type interface{} . The the following example you can see that the logic in main() and Pass() is identical. The only difference is whether the data is a known type or a known type inside an interface{} Go Play: http://play.golang.org/p/FTP3wgc0sZ package main import ( "fmt"

How to get the name of a function in Go?

强颜欢笑 提交于 2019-11-28 15:22:20
Given a function, is it possible to get its name? Say: func foo() { } func GetFunctionName(i interface{}) string { // ... } func main() { // Will print "name: foo" fmt.Println("name:", GetFunctionName(foo)) } I was told that runtime.FuncForPC would help, but I failed to understand how to use it. Sorry for answering my own question, but I found a solution: package main import ( "fmt" "reflect" "runtime" ) func foo() { } func GetFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } func main() { // This will print "name: main.foo" fmt.Println("name:

Convert between slices of different types

房东的猫 提交于 2019-11-28 05:15:49
I get a byte slice ( []byte ) from a UDP socket and want to treat it as an integer slice ( []int32 ) without changing the underlying array, and vice versa. In C(++) I would just cast between pointer types; how would I do this in Go? As others have said, casting the pointer is considered bad form in Go. Here are examples of the proper Go way and the equivalent of the C array casting. WARNING: all code untested. The Right Way In this example, we are using the encoding/binary package to convert each set of 4 bytes into an int32 . This is better because we are specifying the endianness. We are