Last active
August 29, 2015 14:03
-
-
Save wiless/066d0d41da78210d4682 to your computer and use it in GitHub Desktop.
Simple Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
type Car struct | |
{ | |
weight float32 | |
} | |
func CanTalkToAnyOne(dummy interface{}) { | |
str := reflect.TypeOf(dummy).String() | |
obj := reflect.ValueOf(dummy) | |
switch str { | |
case "int": | |
fmt.Printf("Welcome Mr. %s, Take %v", str, obj.Int()) | |
case "float32": | |
fmt.Printf("Seems you are float %f", obj.Float()) | |
default: | |
fmt.Printf("Dont know your type %s", str) | |
fmt.Printf("\nTrying to print native format %v ", obj) | |
} | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
var v int = 1024 | |
var f float32 = 3.1312 | |
var f64 float64 = 3.1312 | |
var maruti Car | |
CanTalkToAnyOne(v) | |
fmt.Printf("\n\n") | |
CanTalkToAnyOne(f) | |
fmt.Printf("\n\n") | |
CanTalkToAnyOne(f64) | |
fmt.Printf("\n\n") | |
CanTalkToAnyOne(maruti) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment