Value to interface

In the same way that we can get reflect.Value from any interface{} value,  we can execute the reverse operation and obtain interface{}from reflect.Value. This is done using the Interface method of the reflected value, and be cast to a concrete type if necessary. If the interested method or function accepts an empty interface, such as json.Marshal or fmt.Println, the returned value can be passed directly without any casting:

func main() {
var a interface{} = int(12)
v := reflect.ValueOf(a)
fmt.Println(v.String())
fmt.Printf("%v", v.Interface())
}

A full example is available here: https://play.golang.org/p/1942Dhm5sap.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253