Tutorial by Examples

import "reflect" value := reflect.ValueOf(4) // Interface returns an interface{}-typed value, which can be type-asserted value.Interface().(int) // 4 // Type gets the reflect.Type, which contains runtime type information about // this value value.Type().Name() // int value.SetInt(5)...
import "reflect" type S struct { A int b string } func (s *S) String() { return s.b } s := &S{ A: 5, b: "example", } indirect := reflect.ValueOf(s) // effectively a pointer to an S value := indirect.Elem() // this is addressable, since we've derefed a point...
import "reflect" s := []int{1, 2, 3} value := reflect.ValueOf(s) value.Len() // 3 value.Index(0).Interface() // 1 value.Type().Kind() // reflect.Slice value.Type().Elem().Name() // int value.Index(1).CanAddr() // true -- slice elements are addressable value....
import "reflect" // this is effectively a pointer dereference x := 5 ptr := reflect.ValueOf(&x) ptr.Type().Name() // *int ptr.Type().Kind() // reflect.Ptr ptr.Interface() // [pointer to x] ptr.Set(4) // panic value := ptr.Elem() // this is a deref value.Type().Name() // int...
reflect.TypeOf can be used to check the type of variables when comparing package main import ( "fmt" "reflect" ) type Data struct { a int } func main() { s:="hey dude" fmt.Println(reflect.TypeOf(...

Page 1 of 1