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.
12 func ExampleMakeFunc() {
13 // swap is the implementation passed to MakeFunc.
14 // It must work in terms of reflect.Values so that it is possible
15 // to write code without knowing beforehand what the types
17 swap
:= func(in
[]reflect
.Value
) []reflect
.Value
{
18 return []reflect
.Value
{in
[1], in
[0]}
21 // makeSwap expects fptr to be a pointer to a nil function.
22 // It sets that pointer to a new function created with MakeFunc.
23 // When the function is invoked, reflect turns the arguments
24 // into Values, calls swap, and then turns swap's result slice
25 // into the values returned by the new function.
26 makeSwap
:= func(fptr
interface{}) {
27 // fptr is a pointer to a function.
28 // Obtain the function value itself (likely nil) as a reflect.Value
29 // so that we can query its type and then set the value.
30 fn
:= reflect
.ValueOf(fptr
).Elem()
32 // Make a function of the right type.
33 v
:= reflect
.MakeFunc(fn
.Type(), swap
)
35 // Assign it to the value fn represents.
39 // Make and call a swap function for ints.
40 var intSwap
func(int, int) (int, int)
42 fmt
.Println(intSwap(0, 1))
44 // Make and call a swap function for float64s.
45 var floatSwap
func(float64, float64) (float64, float64)
47 fmt
.Println(floatSwap(2.72, 3.14))
54 func ExampleStructTag() {
56 F
string `species:"gopher" color:"blue"`
60 st
:= reflect
.TypeOf(s
)
62 fmt
.Println(field
.Tag
.Get("color"), field
.Tag
.Get("species"))