Source file doc/progs/interface2.go

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

View as plain text