go-reflect

Instance new Type (Golang)

▼魔方 西西 提交于 2019-11-27 01:28:24
Can anyone tell me how to create a new instance of Type from a string? Reflect? There are examples but they are for the older (pre Go 1 versions) of the language [:(] So, if I understand your question correctly, you are asking about how you can create an object when you just have the name of the type as string. So, for example, you might have a string "MyStruct" and you want to create an object of this type. Unfortunately, that's not easily possible because Go is a statically typed language and the linker will eliminate dead code (or inline parts of it). So, there is no guarantee, that your

Convert between slices of different types

巧了我就是萌 提交于 2019-11-27 00:51:40
问题 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? 回答1: 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

range over interface{} which stores a slice

谁说我不能喝 提交于 2019-11-26 15:21:58
问题 Given the scenario where you have a function which accepts t interface{} . If it is determined that the t is a slice, how do I range over that slice? I will not know the incoming type, such as []string , []int or []MyType , at compile time. func main() { data := []string{"one","two","three"} test(data) moredata := []int{1,2,3} test(data) } func test(t interface{}) { switch reflect.TypeOf(t).Kind() { case reflect.Slice: // how do I iterate here? for _,value := range t { fmt.Println(value) } }

How do you create a new instance of a struct from its type at run time in Go?

拜拜、爱过 提交于 2019-11-26 15:15:25
问题 In Go, how do you create the instance of an object from its type at run time? I suppose you would also need to get the actual type of the object first too? I am trying to do lazy instantiation to save memory. 回答1: In order to do that you need reflect . package main import ( "fmt" "reflect" ) func main() { // one way is to have a value of the type you want already a := 1 // reflect.New works kind of like the built-in function new // We'll get a reflected pointer to a new int value intPtr :=

Call a Struct and its Method by name in Go?

丶灬走出姿态 提交于 2019-11-26 15:11:37
问题 I have found a function call MethodByName() here http://golang.org/pkg/reflect/#Value.MethodByName but it's not exactly what I want! (maybe because I don't know how to use it ... I cannot find any example with it). What I want is: type MyStruct struct { //some feilds here } func (p *MyStruct) MyMethod { println("My statement."); } CallFunc("MyStruct", "MyMethod"); //print out "My statement." So I guess, first I need something like StructByName() and after that use it for MethodByName() , is

Iterate through the fields of a struct in Go

左心房为你撑大大i 提交于 2019-11-26 12:38:17
问题 Basically, the only way (that I know of) to iterate through the values of the fields of a struct is like this: type Example struct { a_number uint32 a_string string } //... r := &Example{(2 << 31) - 1, \"....\"}: for _, d:= range []interface{}{ r.a_number, r.a_string, } { //do something with the d } I was wondering, if there\'s a better and more versatile way of achieving []interface{}{ r.a_number, r.a_string, } , so I don\'t need to list each parameter individually, or alternatively, is

Using reflect, how do you set the value of a struct field?

会有一股神秘感。 提交于 2019-11-26 11:48:21
问题 having a rough time working with struct fields using reflect package. in particular, have not figured out how to set the field value. type t struct { fi int; fs string } var r t = t{ 123, \"jblow\" } var i64 int64 = 456 getting Name of field i - this seems to work var field = reflect.TypeOf(r).Field(i).Name getting value of field i as a) interface{}, b) int - this seems to work var iface interface{} = reflect.ValueOf(r).Field(i).Interface() var i int = int(reflect.ValueOf(r).Field(i).Int())

Instance new Type (Golang)

耗尽温柔 提交于 2019-11-26 09:39:52
问题 Can anyone tell me how to create a new instance of Type from a string? Reflect? There are examples but they are for the older (pre Go 1 versions) of the language [:(] 回答1: So, if I understand your question correctly, you are asking about how you can create an object when you just have the name of the type as string. So, for example, you might have a string "MyStruct" and you want to create an object of this type. Unfortunately, that's not easily possible because Go is a statically typed

How to compare if two structs, slices or maps are equal?

拜拜、爱过 提交于 2019-11-26 09:17:18
问题 I want to check if two structs, slices and maps are equal. But I\'m running into problems with the following code. See my comments at the relevant lines. package main import ( \"fmt\" \"reflect\" ) type T struct { X int Y string Z []int M map[string]int } func main() { t1 := T{ X: 1, Y: \"lei\", Z: []int{1, 2, 3}, M: map[string]int{ \"a\": 1, \"b\": 2, }, } t2 := T{ X: 1, Y: \"lei\", Z: []int{1, 2, 3}, M: map[string]int{ \"a\": 1, \"b\": 2, }, } fmt.Println(t2 == t1) //error - invalid

Type converting slices of interfaces

余生长醉 提交于 2019-11-25 22:08:29
问题 I\'m curious why Go does\'t implicitly convert []T to []interface{} when it will implicitly convert T to interface{} . Is there something non-trivial about this conversion that I\'m missing? Example: func foo([]interface{}) { /* do something */ } func main() { var a []string = []string{\"hello\", \"world\"} foo(a) } go build complains cannot use a (type []string) as type []interface {} in function argument And if I try to do it explicitly, same thing: b := []interface{}(a) complains cannot