Skip to content

Instantly share code, notes, and snippets.

@serverhorror
Created August 15, 2012 09:03
Show Gist options
  • Select an option

  • Save serverhorror/3357838 to your computer and use it in GitHub Desktop.

Select an option

Save serverhorror/3357838 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
)
type Value struct {
Name string
Value interface{}
}
func (v Value) String() string {
return fmt.Sprintf("<<%s,%v>>", v.Name, v.Value)
}
func main() {
var I []interface{}
// Just in a block for readability, no other reason...
{
I = append(I, 3)
I = append(I, Value{Name: "int", Value: 1})
I = append(I, Value{Name: "int32", Value: 'A'})
I = append(I, Value{Name: "float64", Value: 1e-1})
I = append(I, Value{Name: "complex128", Value: (1e-1 + 1e0i)})
}
for _, i := range I {
switch i.(type) {
case int, int8, int32, int64:
log.Printf("Type: %T, Value: %v", i, i)
case float32, float64:
log.Printf("Type: %T, Value: %v", i, i)
case complex64, complex128:
log.Printf("Type: %T, Value: %v", i, i)
case string:
log.Printf("Type: %T, Value: %v", i, i)
case byte:
log.Printf("Type: %T, Value: %v", i, i)
case Value:
if iv, ok := i.(Value); ok {
switch iv.Value.(type) {
case int, int8, int32, int64:
log.Printf("Value.Value:: Type: %T, Value: %v (iv::%v)", iv.Value, iv.Value, iv)
case float32, float64:
log.Printf("Value.Value:: Type: %T, Value: %v (iv::%v)", iv.Value, iv.Value, iv)
case complex64, complex128:
log.Printf("Value.Value:: Type: %T, Value: %v (iv::%v)", iv.Value, iv.Value, iv)
default:
log.Fatalf("Value.Value:: Case not handled (%v::%v)", iv, ok)
}
} else {
log.Fatalf("i.(Value):: Case not handled (%v::%v)", iv, ok)
}
default:
log.Fatal("Case not handled")
}
}
log.Print("Everything went fine!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment