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.
16 func ExampleMakeFunc() {
17 // swap is the implementation passed to MakeFunc.
18 // It must work in terms of reflect.Values so that it is possible
19 // to write code without knowing beforehand what the types
21 swap
:= func(in
[]reflect
.Value
) []reflect
.Value
{
22 return []reflect
.Value
{in
[1], in
[0]}
25 // makeSwap expects fptr to be a pointer to a nil function.
26 // It sets that pointer to a new function created with MakeFunc.
27 // When the function is invoked, reflect turns the arguments
28 // into Values, calls swap, and then turns swap's result slice
29 // into the values returned by the new function.
30 makeSwap
:= func(fptr
interface{}) {
31 // fptr is a pointer to a function.
32 // Obtain the function value itself (likely nil) as a reflect.Value
33 // so that we can query its type and then set the value.
34 fn
:= reflect
.ValueOf(fptr
).Elem()
36 // Make a function of the right type.
37 v
:= reflect
.MakeFunc(fn
.Type(), swap
)
39 // Assign it to the value fn represents.
43 // Make and call a swap function for ints.
44 var intSwap
func(int, int) (int, int)
46 fmt
.Println(intSwap(0, 1))
48 // Make and call a swap function for float64s.
49 var floatSwap
func(float64, float64) (float64, float64)
51 fmt
.Println(floatSwap(2.72, 3.14))
58 func ExampleStructTag() {
60 F
string `species:"gopher" color:"blue"`
64 st
:= reflect
.TypeOf(s
)
66 fmt
.Println(field
.Tag
.Get("color"), field
.Tag
.Get("species"))
72 func ExampleStructTag_Lookup() {
74 F0
string `alias:"field_0"`
80 st
:= reflect
.TypeOf(s
)
81 for i
:= 0; i
< st
.NumField(); i
++ {
83 if alias
, ok
:= field
.Tag
.Lookup("alias"); ok
{
85 fmt
.Println("(blank)")
90 fmt
.Println("(not specified)")
100 func ExampleTypeOf() {
101 // As interface types are only used for static typing, a
102 // common idiom to find the reflection Type for an interface
103 // type Foo is to use a *Foo value.
104 writerType
:= reflect
.TypeOf((*io
.Writer
)(nil)).Elem()
106 fileType
:= reflect
.TypeOf((*os
.File
)(nil))
107 fmt
.Println(fileType
.Implements(writerType
))
113 func ExampleStructOf() {
114 typ
:= reflect
.StructOf([]reflect
.StructField
{
117 Type
: reflect
.TypeOf(float64(0)),
118 Tag
: `json:"height"`,
122 Type
: reflect
.TypeOf(int(0)),
127 v
:= reflect
.New(typ
).Elem()
128 v
.Field(0).SetFloat(0.4)
130 s
:= v
.Addr().Interface()
132 w
:= new(bytes
.Buffer
)
133 if err
:= json
.NewEncoder(w
).Encode(s
); err
!= nil {
137 fmt
.Printf("value: %+v\n", s
)
138 fmt
.Printf("json: %s", w
.Bytes())
140 r
:= bytes
.NewReader([]byte(`{"height":1.5,"age":10}`))
141 if err
:= json
.NewDecoder(r
).Decode(s
); err
!= nil {
144 fmt
.Printf("value: %+v\n", s
)
147 // value: &{Height:0.4 Age:2}
148 // json: {"height":0.4,"age":2}
149 // value: &{Height:1.5 Age:10}