diff options
Diffstat (limited to 'doc/progs/interface2.go')
-rw-r--r-- | doc/progs/interface2.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/doc/progs/interface2.go b/doc/progs/interface2.go index e2716cf16..a541d94e4 100644 --- a/doc/progs/interface2.go +++ b/doc/progs/interface2.go @@ -1,3 +1,9 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains the code snippets included in "The Laws of Reflection." + package main import ( @@ -39,11 +45,14 @@ func f3() { type MyInt int var x MyInt = 7 v := reflect.ValueOf(x) + // STOP OMIT // START f3b OMIT y := v.Interface().(float64) // y will have type float64. fmt.Println(y) + // STOP OMIT // START f3c OMIT fmt.Println(v.Interface()) + // STOP OMIT // START f3d OMIT fmt.Printf("value is %7.1e\n", v.Interface()) // STOP OMIT @@ -69,6 +78,7 @@ func f6() { // START f6 OMIT var x float64 = 3.4 v := reflect.ValueOf(x) + // STOP OMIT // START f6b OMIT v.SetFloat(7.1) // STOP OMIT @@ -80,9 +90,11 @@ func f7() { p := reflect.ValueOf(&x) // Note: take the address of x. fmt.Println("type of p:", p.Type()) fmt.Println("settability of p:", p.CanSet()) + // STOP OMIT // START f7b OMIT v := p.Elem() fmt.Println("settability of v:", v.CanSet()) + // STOP OMIT // START f7c OMIT v.SetFloat(7.1) fmt.Println(v.Interface()) @@ -104,9 +116,17 @@ func f8() { fmt.Printf("%d: %s %s = %v\n", i, typeOfT.Field(i).Name, f.Type(), f.Interface()) } + // STOP OMIT // START f8b OMIT s.Field(0).SetInt(77) s.Field(1).SetString("Sunset Strip") fmt.Println("t is now", t) // STOP OMIT } + +func f9() { + // START f9 OMIT + var x float64 = 3.4 + fmt.Println("value:", reflect.ValueOf(x)) + // STOP OMIT +} |