interface2.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file contains the code snippets included in "The Laws of Reflection."
  5. package main
  6. import (
  7. "fmt"
  8. "reflect"
  9. )
  10. func main() {
  11. var x float64 = 3.4
  12. fmt.Println("type:", reflect.TypeOf(x))
  13. // STOP OMIT
  14. // TODO(proppy): test output OMIT
  15. }
  16. // STOP main OMIT
  17. func f1() {
  18. // START f1 OMIT
  19. var x float64 = 3.4
  20. v := reflect.ValueOf(x)
  21. fmt.Println("type:", v.Type())
  22. fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
  23. fmt.Println("value:", v.Float())
  24. // STOP OMIT
  25. }
  26. func f2() {
  27. // START f2 OMIT
  28. var x uint8 = 'x'
  29. v := reflect.ValueOf(x)
  30. fmt.Println("type:", v.Type()) // uint8.
  31. fmt.Println("kind is uint8: ", v.Kind() == reflect.Uint8) // true.
  32. x = uint8(v.Uint()) // v.Uint returns a uint64.
  33. // STOP OMIT
  34. }
  35. func f3() {
  36. // START f3 OMIT
  37. type MyInt int
  38. var x MyInt = 7
  39. v := reflect.ValueOf(x)
  40. // STOP OMIT
  41. // START f3b OMIT
  42. y := v.Interface().(float64) // y will have type float64.
  43. fmt.Println(y)
  44. // STOP OMIT
  45. // START f3c OMIT
  46. fmt.Println(v.Interface())
  47. // STOP OMIT
  48. // START f3d OMIT
  49. fmt.Printf("value is %7.1e\n", v.Interface())
  50. // STOP OMIT
  51. }
  52. func f4() {
  53. // START f4 OMIT
  54. var x float64 = 3.4
  55. v := reflect.ValueOf(x)
  56. v.SetFloat(7.1) // Error: will panic.
  57. // STOP OMIT
  58. }
  59. func f5() {
  60. // START f5 OMIT
  61. var x float64 = 3.4
  62. v := reflect.ValueOf(x)
  63. fmt.Println("settability of v:", v.CanSet())
  64. // STOP OMIT
  65. }
  66. func f6() {
  67. // START f6 OMIT
  68. var x float64 = 3.4
  69. v := reflect.ValueOf(x)
  70. // STOP OMIT
  71. // START f6b OMIT
  72. v.SetFloat(7.1)
  73. // STOP OMIT
  74. }
  75. func f7() {
  76. // START f7 OMIT
  77. var x float64 = 3.4
  78. p := reflect.ValueOf(&x) // Note: take the address of x.
  79. fmt.Println("type of p:", p.Type())
  80. fmt.Println("settability of p:", p.CanSet())
  81. // STOP OMIT
  82. // START f7b OMIT
  83. v := p.Elem()
  84. fmt.Println("settability of v:", v.CanSet())
  85. // STOP OMIT
  86. // START f7c OMIT
  87. v.SetFloat(7.1)
  88. fmt.Println(v.Interface())
  89. fmt.Println(x)
  90. // STOP OMIT
  91. }
  92. func f8() {
  93. // START f8 OMIT
  94. type T struct {
  95. A int
  96. B string
  97. }
  98. t := T{23, "skidoo"}
  99. s := reflect.ValueOf(&t).Elem()
  100. typeOfT := s.Type()
  101. for i := 0; i < s.NumField(); i++ {
  102. f := s.Field(i)
  103. fmt.Printf("%d: %s %s = %v\n", i,
  104. typeOfT.Field(i).Name, f.Type(), f.Interface())
  105. }
  106. // STOP OMIT
  107. // START f8b OMIT
  108. s.Field(0).SetInt(77)
  109. s.Field(1).SetString("Sunset Strip")
  110. fmt.Println("t is now", t)
  111. // STOP OMIT
  112. }
  113. func f9() {
  114. // START f9 OMIT
  115. var x float64 = 3.4
  116. fmt.Println("value:", reflect.ValueOf(x))
  117. // STOP OMIT
  118. }