Skip to content

Instantly share code, notes, and snippets.

@wiless
Created March 15, 2015 13:51
Show Gist options
  • Save wiless/2945dbd222bd3a150eaf to your computer and use it in GitHub Desktop.
Save wiless/2945dbd222bd3a150eaf to your computer and use it in GitHub Desktop.
Example walk through interface and reflect
package main
import (
"reflect"
"strconv"
"fmt"
)
func sum(arguments ...interface{}) {
fmt.Printf("\n Input argument Type is = %T", arguments)
fmt.Printf("\n Input argument Raw Value is = %v", arguments)
total := 0.0
fmt.Printf("\n ==========================================")
for i := 0; i < len(arguments); i++ {
fmt.Printf("\n %d : args Type is = %T", i, arguments[i])
mytype := fmt.Sprintf("%T", arguments[i])
convertedvalue, ok := arguments[i].(float64)
if !ok {
fmt.Println("\nSorry sendil i could not sum an unknown which cannot be conveted to float64")
// break
switch mytype {
case "int":
fmt.Println("\nCought you.. you are int I can still convert to float")
convertedvalue = float64(arguments[i].(int))
case "string":
fmt.Println("\nCought you.. you are string \n I can attempt to convert to float")
inputstring := arguments[i].(string)
temp, err := strconv.ParseFloat(inputstring, 64)
if err == nil {
convertedvalue = temp
} else {
fmt.Printf("\n Sorry could not convertd : error = %v", err)
}
default:
typeinfo := reflect.TypeOf(arguments[i])
something := reflect.TypeOf(arguments[i]).Kind()
fmt.Printf("\n Kind of Reflect type is %v", something)
if something == reflect.Struct {
valueOfInterface := reflect.ValueOf(arguments[i])
fmt.Println("\n you are STRUCT.. i will dissect and find a float in you ")
fmt.Printf("\n You have %d ELEMENTS ", typeinfo.NumField())
fmt.Printf("\n You have %d Methods ", typeinfo.NumMethod())
_, xok := arguments[i].(fmt.Stringer)
fmt.Printf("\nWhat are you checking , Found = %v", xok)
/// attempting to call a method
for i := 0; i < typeinfo.NumMethod(); i++ {
fmt.Printf("\n Method %d : Info = %#v ", i, typeinfo.Method(i))
fmt.Printf("\n Method %d : Being called ", i)
valueOfInterface.Method(i).Call([]reflect.Value{})
}
for f := 0; f < typeinfo.NumField(); f++ {
fmt.Printf("\n Field %d : Info is %#v ", f, typeinfo.Field(f))
// Check if can be convetted to Float64
fmt.Printf("\n Field %d : Kind is %v ", f, typeinfo.Field(f).Type.Kind())
fmt.Printf("\n Field %d : Value is %v ", f, valueOfInterface.Field(f))
if typeinfo.Field(f).Type.Kind() == reflect.Float64 || typeinfo.Field(f).Type.Kind() == reflect.Float32 {
convertedvalue = valueOfInterface.Field(f).Float()
fmt.Printf("\n I found a float value in %s Field of your structure %v", typeinfo.Field(f).Name, convertedvalue)
}
}
}
fmt.Println("\nSorry boss you are not known to this world")
}
}
fmt.Printf("\n %d : converted , value is %f", i, convertedvalue)
total = total + convertedvalue
fmt.Printf("\n =====================================")
}
fmt.Println("\n Result is ", total)
}
type IQLevel float64
type Person struct {
FirstName string `ssk:useInitials`
Age float32
}
func (p Person) Hi() {
fmt.Printf("\n Hello whosover , I am %s, and %f old ", p.FirstName, p.Age)
}
func (p Person) String() string {
return fmt.Sprintf("Hello whosover , I am %s, and %f old ", p.FirstName, p.Age)
}
func main() {
fmt.Printf("\n hello World : Your SYSTEM has CPU \n")
// y := 3.4
myself := Person{"sendil", 22.2}
// suvrat := Person{"Suvrat", 23.1}
myiq := IQLevel(322.2)
sum(11.2, "300.123", 100, myself, myiq)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment