I've tried to identify a struct with string value(name).
reflect.TypeOf
returns Type
.
But type assertion needs a type
.
How can I casting Type
to type
?
Or any suggestion to handle it?
http://play.golang.org/p/3PJG3YxIyf
package main
import (
"fmt"
"reflect"
)
type Article struct {
Id int64 `json:"id"`
Title string `json:"title",sql:"size:255"`
Content string `json:"content"`
}
func IdentifyItemType(name string) interface{} {
var item interface{}
switch name {
default:
item = Article{}
}
return item
}
func main() {
i := IdentifyItemType("name")
item := i.(Article)
fmt.Printf("Hello, item : %v\n", item)
item2 := i.(reflect.TypeOf(i)) // reflect.TypeOf(i) is not a type
fmt.Printf("Hello, item2 : %v\n", item2)
}
To achieve this, it uses type assertion, using the “comma, ok” syntax. If the type assertion succeeds, the function calls the String() method on the value. Running the above program will indeed output the print statement of line 19 since the parameter satisfies the fmt. Stringer interface.
Reflection acts on three important reflection properties that every Golang object has: Type, Kind, and Value. 'Kind' can be one of struct, int, string, slice, map, or one of the other Golang primitives. The reflect package also allows you to modify the value of a specific field given the reflected value of a field.
Reflection is the ability of a program to introspect and analyze its structure during run-time. In Go language, reflection is primarily carried out with types. The reflect package offers all the required APIs/Methods for this purpose. Reflection is often termed as a method of metaprogramming.
If you need to switch on the type of the outer interface{} you wont need reflection.
switch x.(type){
case int:
dosomething()
}
...but if you need to switch on the type of the attributes in an interface then you can do this:
s := reflect.ValueOf(x)
for i:=0; i<s.NumValues; i++{
switch s.Field(i).Interface().(type){
case int:
dosomething()
}
}
I haven't found a cleaner way, I'd love to know if it exists.
A type assertion, syntactically, takes a type in the parentheses, not an expression. So it is a syntax error.
You seem to be trying to do a type assertion with a value computed at runtime. Does that make sense? Let's think about what a type assertion is.
A type assertion consists of two things:
x.(T)
has compile-time type T
. This allows you to do stuff the expression that you can do with type T
, which you may not be able to do with the type of x
.nil
and is actually of the given type, and if not, it causes a panic.The first part obviously doesn't make sense for a type computed at runtime. The compile-time type of the resulting expression cannot depend on something that is not known at compile-time.
The second one (runtime check) can be done with a type computed at runtime. Something like:
if reflect.TypeOf(x) != someTypeComputedAtRuntime {
panic(42)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With