reflect1.go 552 B

123456789101112131415161718192021222324252627282930313233
  1. // reflect1.go
  2. // blog: Laws of Reflection
  3. package main
  4. import (
  5. "fmt"
  6. "reflect"
  7. )
  8. func main() {
  9. var x float64 = 3.4
  10. fmt.Println("type:", reflect.TypeOf(x))
  11. v := reflect.ValueOf(x)
  12. fmt.Println("value:", v)
  13. fmt.Println("type:", v.Type())
  14. fmt.Println("kind:", v.Kind())
  15. fmt.Println("value:", v.Float())
  16. fmt.Println(v.Interface())
  17. fmt.Printf("value is %5.2e\n", v.Interface())
  18. y := v.Interface().(float64)
  19. fmt.Println(y)
  20. }
  21. /* output:
  22. type: float64
  23. value: <float64 Value>
  24. type: float64
  25. kind: float64
  26. value: 3.4
  27. 3.4
  28. value is 3.40e+00
  29. 3.4
  30. */