Skip to content

Instantly share code, notes, and snippets.

@wiless
Last active August 29, 2015 14:03
Show Gist options
  • Save wiless/066d0d41da78210d4682 to your computer and use it in GitHub Desktop.
Save wiless/066d0d41da78210d4682 to your computer and use it in GitHub Desktop.
Simple Interface
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