Reflection Basics
Type and Kind
Reflection can inspect the runtime type and broad kind of a value.
Type and Kind
type_kind.go
package main
import (
"fmt"
"reflect"
)
func main() {
var sample =
valueType := reflect.TypeOf(sample)
kind := valueType.Kind()
size := valueType.Size()
fmt.Println("sample=", sample)
fmt.Println("type=", valueType.String())
fmt.Println("kind=", kind.String())
fmt.Println("size=", size)
}
package main
import (
"fmt"
"reflect"
)
func main() {
var sample =
valueType := reflect.TypeOf(sample)
kind := valueType.Kind()
size := valueType.Size()
fmt.Println("sample=", sample)
fmt.Println("type=", valueType.String())
fmt.Println("kind=", kind.String())
fmt.Println("size=", size)
}
package main
import (
"fmt"
"reflect"
)
func main() {
var sample =
valueType := reflect.TypeOf(sample)
kind := valueType.Kind()
size := valueType.Size()
fmt.Println("sample=", sample)
fmt.Println("type=", valueType.String())
fmt.Println("kind=", kind.String())
fmt.Println("size=", size)
}
type and kind
`reflect.TypeOf` reports the concrete type, while `Kind` groups types into categories like string, int, struct, and slice.