Golang reflect.Indirect() function example
package reflect
Golang reflect.Indirect() function usage example.
NOTE : Useful in a situation where you want to probe or evaluate a variable type first and then use select-case statement to handle different data type.
package main
import (
"fmt"
"reflect"
)
func main() {
var i []int // slice
var str [3]string // array
var m = make(map[int]string) //map
var iValue reflect.Value = reflect.ValueOf(&i)
fmt.Println("&i type : ", iValue.Kind()) // ptr or pointer type of because &i
indirectI := reflect.Indirect(iValue)
fmt.Println("i type : ", indirectI.Kind())
fmt.Println("-----------------------------------------")
var strValue reflect.Value = reflect.ValueOf(&str)
fmt.Println("&str type : ", strValue.Kind())
indirectStr := reflect.Indirect(strValue)
fmt.Println("str type : ", indirectStr.Kind())
fmt.Println("-----------------------------------------")
var mValue reflect.Value = reflect.ValueOf(&m)
fmt.Println("&m type : ", mValue.Kind())
indirectM := reflect.Indirect(mValue)
fmt.Println("m type : ", indirectM.Kind())
}
Output :
&i type : ptr
i type : slice
&str type : ptr
str type : array
&m type : ptr
m type : map
Reference :
Advertisement
Something interesting
Tutorials
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+15.8k Golang : How to login and logout with JWT example
+15.3k Golang : How to get Unix file descriptor for console and file
+16.1k Golang : How to check if input from os.Args is integer?
+6k PHP : How to check if an array is empty ?
+7.5k Golang : Rename part of filename
+11.2k Google Maps URL parameters configuration
+18.4k Golang : How to get hour, minute, second from time?
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+16.5k Golang : Get IP addresses of a domain name
+11.1k Golang : Simple image viewer with Go-GTK
+27.5k Golang : dial tcp: too many colons in address