github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/doc/articles/laws_of_reflection.html (about) 1 <!--{ 2 "Title": "The Laws of Reflection", 3 "Template": true 4 }--> 5 6 <p> 7 Reflection in computing is the 8 ability of a program to examine its own structure, particularly 9 through types; it's a form of metaprogramming. It's also a great 10 source of confusion. 11 </p> 12 13 <p> 14 In this article we attempt to clarify things by explaining how 15 reflection works in Go. Each language's reflection model is 16 different (and many languages don't support it at all), but 17 this article is about Go, so for the rest of this article the word 18 "reflection" should be taken to mean "reflection in Go". 19 </p> 20 21 <p><b>Types and interfaces</b></p> 22 23 <p> 24 Because reflection builds on the type system, let's start with a 25 refresher about types in Go. 26 </p> 27 28 <p> 29 Go is statically typed. Every variable has a static type, that is, 30 exactly one type known and fixed at compile time: <code>int</code>, 31 <code>float32</code>, <code>*MyType</code>, <code>[]byte</code>, 32 and so on. If we declare 33 </p> 34 35 {{code "/doc/progs/interface.go" `/type MyInt/` `/STOP/`}} 36 37 <p> 38 then <code>i</code> has type <code>int</code> and <code>j</code> 39 has type <code>MyInt</code>. The variables <code>i</code> and 40 <code>j</code> have distinct static types and, although they have 41 the same underlying type, they cannot be assigned to one another 42 without a conversion. 43 </p> 44 45 <p> 46 One important category of type is interface types, which represent 47 fixed sets of methods. An interface variable can store any concrete 48 (non-interface) value as long as that value implements the 49 interface's methods. A well-known pair of examples is 50 <code>io.Reader</code> and <code>io.Writer</code>, the types 51 <code>Reader</code> and <code>Writer</code> from the 52 <a href="/pkg/io/">io package</a>: 53 </p> 54 55 {{code "/doc/progs/interface.go" `/// Reader/` `/STOP/`}} 56 57 <p> 58 Any type that implements a <code>Read</code> (or 59 <code>Write</code>) method with this signature is said to implement 60 <code>io.Reader</code> (or <code>io.Writer</code>). For the 61 purposes of this discussion, that means that a variable of type 62 <code>io.Reader</code> can hold any value whose type has a 63 <code>Read</code> method: 64 </p> 65 66 {{code "/doc/progs/interface.go" `/func readers/` `/STOP/`}} 67 68 <p> 69 It's important to be clear that whatever concrete value 70 <code>r</code> may hold, <code>r</code>'s type is always 71 <code>io.Reader</code>: Go is statically typed and the static type 72 of <code>r</code> is <code>io.Reader</code>.</p> 73 74 <p> 75 An extremely important example of an interface type is the empty 76 interface: 77 </p> 78 79 <pre> 80 interface{} 81 </pre> 82 83 <p> 84 It represents the empty set of methods and is satisfied by any 85 value at all, since any value has zero or more methods. 86 </p> 87 88 <p> 89 Some people say that Go's interfaces are dynamically typed, but 90 that is misleading. They are statically typed: a variable of 91 interface type always has the same static type, and even though at 92 run time the value stored in the interface variable may change 93 type, that value will always satisfy the interface. 94 </p> 95 96 <p> 97 We need to be precise about all this because reflection and 98 interfaces are closely related. 99 </p> 100 101 <p><b>The representation of an interface</b></p> 102 103 <p> 104 Russ Cox has written a 105 <a href="http://research.swtch.com/2009/12/go-data-structures-interfaces.html">detailed blog post</a> 106 about the representation of interface values in Go. It's not necessary to 107 repeat the full story here, but a simplified summary is in order. 108 </p> 109 110 <p> 111 A variable of interface type stores a pair: the concrete value 112 assigned to the variable, and that value's type descriptor. 113 To be more precise, the value is the underlying concrete data item 114 that implements the interface and the type describes the full type 115 of that item. For instance, after 116 </p> 117 118 {{code "/doc/progs/interface.go" `/func typeAssertions/` `/STOP/`}} 119 120 <p> 121 <code>r</code> contains, schematically, the (value, type) pair, 122 (<code>tty</code>, <code>*os.File</code>). Notice that the type 123 <code>*os.File</code> implements methods other than 124 <code>Read</code>; even though the interface value provides access 125 only to the <code>Read</code> method, the value inside carries all 126 the type information about that value. That's why we can do things 127 like this: 128 </p> 129 130 {{code "/doc/progs/interface.go" `/var w io.Writer/` `/STOP/`}} 131 132 <p> 133 The expression in this assignment is a type assertion; what it 134 asserts is that the item inside <code>r</code> also implements 135 <code>io.Writer</code>, and so we can assign it to <code>w</code>. 136 After the assignment, <code>w</code> will contain the pair 137 (<code>tty</code>, <code>*os.File</code>). That's the same pair as 138 was held in <code>r</code>. The static type of the interface 139 determines what methods may be invoked with an interface variable, 140 even though the concrete value inside may have a larger set of 141 methods. 142 </p> 143 144 <p> 145 Continuing, we can do this: 146 </p> 147 148 {{code "/doc/progs/interface.go" `/var empty interface{}/` `/STOP/`}} 149 150 <p> 151 and our empty interface value <code>e</code> will again contain 152 that same pair, (<code>tty</code>, <code>*os.File</code>). That's 153 handy: an empty interface can hold any value and contains all the 154 information we could ever need about that value. 155 </p> 156 157 <p> 158 (We don't need a type assertion here because it's known statically 159 that <code>w</code> satisfies the empty interface. In the example 160 where we moved a value from a <code>Reader</code> to a 161 <code>Writer</code>, we needed to be explicit and use a type 162 assertion because <code>Writer</code>'s methods are not a 163 subset of <code>Reader</code>'s.) 164 </p> 165 166 <p> 167 One important detail is that the pair inside an interface always 168 has the form (value, concrete type) and cannot have the form 169 (value, interface type). Interfaces do not hold interface 170 values. 171 </p> 172 173 <p> 174 Now we're ready to reflect. 175 </p> 176 177 <p><b>The first law of reflection</b></p> 178 179 <p><b>1. Reflection goes from interface value to reflection object.</b></p> 180 181 <p> 182 At the basic level, reflection is just a mechanism to examine the 183 type and value pair stored inside an interface variable. To get 184 started, there are two types we need to know about in 185 <a href="/pkg/reflect/">package reflect</a>: 186 <a href="/pkg/reflect/#Type">Type</a> and 187 <a href="/pkg/reflect/#Value">Value</a>. Those two types 188 give access to the contents of an interface variable, and two 189 simple functions, called <code>reflect.TypeOf</code> and 190 <code>reflect.ValueOf</code>, retrieve <code>reflect.Type</code> 191 and <code>reflect.Value</code> pieces out of an interface value. 192 (Also, from the <code>reflect.Value</code> it's easy to get 193 to the <code>reflect.Type</code>, but let's keep the 194 <code>Value</code> and <code>Type</code> concepts separate for 195 now.) 196 </p> 197 198 <p> 199 Let's start with <code>TypeOf</code>: 200 </p> 201 202 {{code "/doc/progs/interface2.go" `/package main/` `/STOP main/`}} 203 204 <p> 205 This program prints 206 </p> 207 208 <pre> 209 type: float64 210 </pre> 211 212 <p> 213 You might be wondering where the interface is here, since the program looks 214 like it's passing the <code>float64</code> variable <code>x</code>, not an 215 interface value, to <code>reflect.TypeOf</code>. But it's there; as 216 <a href="/pkg/reflect/#TypeOf">godoc reports</a>, the signature of 217 <code>reflect.TypeOf</code> includes an empty interface: 218 </p> 219 220 <pre> 221 // TypeOf returns the reflection Type of the value in the interface{}. 222 func TypeOf(i interface{}) Type 223 </pre> 224 225 <p> 226 When we call <code>reflect.TypeOf(x)</code>, <code>x</code> is 227 first stored in an empty interface, which is then passed as the 228 argument; <code>reflect.TypeOf</code> unpacks that empty interface 229 to recover the type information. 230 </p> 231 232 <p> 233 The <code>reflect.ValueOf</code> function, of course, recovers the 234 value (from here on we'll elide the boilerplate and focus just on 235 the executable code): 236 </p> 237 238 {{code "/doc/progs/interface2.go" `/START f9/` `/STOP/`}} 239 240 <p> 241 prints 242 </p> 243 244 <pre> 245 value: <float64 Value> 246 </pre> 247 248 <p> 249 Both <code>reflect.Type</code> and <code>reflect.Value</code> have 250 lots of methods to let us examine and manipulate them. One 251 important example is that <code>Value</code> has a 252 <code>Type</code> method that returns the <code>Type</code> of a 253 <code>reflect.Value</code>. Another is that both <code>Type</code> 254 and <code>Value</code> have a <code>Kind</code> method that returns 255 a constant indicating what sort of item is stored: 256 <code>Uint</code>, <code>Float64</code>, <code>Slice</code>, and so 257 on. Also methods on <code>Value</code> with names like 258 <code>Int</code> and <code>Float</code> let us grab values (as 259 <code>int64</code> and <code>float64</code>) stored inside: 260 </p> 261 262 {{code "/doc/progs/interface2.go" `/START f1/` `/STOP/`}} 263 264 <p> 265 prints 266 </p> 267 268 <pre> 269 type: float64 270 kind is float64: true 271 value: 3.4 272 </pre> 273 274 <p> 275 There are also methods like <code>SetInt</code> and 276 <code>SetFloat</code> but to use them we need to understand 277 settability, the subject of the third law of reflection, discussed 278 below. 279 </p> 280 281 <p> 282 The reflection library has a couple of properties worth singling 283 out. First, to keep the API simple, the "getter" and "setter" 284 methods of <code>Value</code> operate on the largest type that can 285 hold the value: <code>int64</code> for all the signed integers, for 286 instance. That is, the <code>Int</code> method of 287 <code>Value</code> returns an <code>int64</code> and the 288 <code>SetInt</code> value takes an <code>int64</code>; it may be 289 necessary to convert to the actual type involved: 290 </p> 291 292 {{code "/doc/progs/interface2.go" `/START f2/` `/STOP/`}} 293 294 <p> 295 The second property is that the <code>Kind</code> of a reflection 296 object describes the underlying type, not the static type. If a 297 reflection object contains a value of a user-defined integer type, 298 as in 299 </p> 300 301 {{code "/doc/progs/interface2.go" `/START f3/` `/STOP/`}} 302 303 <p> 304 the <code>Kind</code> of <code>v</code> is still 305 <code>reflect.Int</code>, even though the static type of 306 <code>x</code> is <code>MyInt</code>, not <code>int</code>. In 307 other words, the <code>Kind</code> cannot discriminate an int from 308 a <code>MyInt</code> even though the <code>Type</code> can. 309 </p> 310 311 <p><b>The second law of reflection</b></p> 312 313 <p><b>2. Reflection goes from reflection object to interface 314 value.</b></p> 315 316 <p> 317 Like physical reflection, reflection in Go generates its own 318 inverse. 319 </p> 320 321 <p> 322 Given a <code>reflect.Value</code> we can recover an interface 323 value using the <code>Interface</code> method; in effect the method 324 packs the type and value information back into an interface 325 representation and returns the result: 326 </p> 327 328 <pre> 329 // Interface returns v's value as an interface{}. 330 func (v Value) Interface() interface{} 331 </pre> 332 333 <p> 334 As a consequence we can say 335 </p> 336 337 {{code "/doc/progs/interface2.go" `/START f3b/` `/STOP/`}} 338 339 <p> 340 to print the <code>float64</code> value represented by the 341 reflection object <code>v</code>. 342 </p> 343 344 <p> 345 We can do even better, though. The arguments to 346 <code>fmt.Println</code>, <code>fmt.Printf</code> and so on are all 347 passed as empty interface values, which are then unpacked by the 348 <code>fmt</code> package internally just as we have been doing in 349 the previous examples. Therefore all it takes to print the contents 350 of a <code>reflect.Value</code> correctly is to pass the result of 351 the <code>Interface</code> method to the formatted print 352 routine: 353 </p> 354 355 {{code "/doc/progs/interface2.go" `/START f3c/` `/STOP/`}} 356 357 <p> 358 (Why not <code>fmt.Println(v)</code>? Because <code>v</code> is a 359 <code>reflect.Value</code>; we want the concrete value it holds.) 360 Since our value is a <code>float64</code>, we can even use a 361 floating-point format if we want: 362 </p> 363 364 {{code "/doc/progs/interface2.go" `/START f3d/` `/STOP/`}} 365 366 <p> 367 and get in this case 368 </p> 369 370 <pre> 371 3.4e+00 372 </pre> 373 374 <p> 375 Again, there's no need to type-assert the result of 376 <code>v.Interface()</code> to <code>float64</code>; the empty 377 interface value has the concrete value's type information inside 378 and <code>Printf</code> will recover it. 379 </p> 380 381 <p> 382 In short, the <code>Interface</code> method is the inverse of the 383 <code>ValueOf</code> function, except that its result is always of 384 static type <code>interface{}</code>. 385 </p> 386 387 <p> 388 Reiterating: Reflection goes from interface values to reflection 389 objects and back again. 390 </p> 391 392 <p><b>The third law of reflection</b></p> 393 394 <p><b>3. To modify a reflection object, the value must be settable.</b></p> 395 396 <p> 397 The third law is the most subtle and confusing, but it's easy 398 enough to understand if we start from first principles. 399 </p> 400 401 <p> 402 Here is some code that does not work, but is worth studying. 403 </p> 404 405 {{code "/doc/progs/interface2.go" `/START f4/` `/STOP/`}} 406 407 <p> 408 If you run this code, it will panic with the cryptic message 409 </p> 410 411 <pre> 412 panic: reflect.Value.SetFloat using unaddressable value 413 </pre> 414 415 <p> 416 The problem is not that the value <code>7.1</code> is not 417 addressable; it's that <code>v</code> is not settable. Settability 418 is a property of a reflection <code>Value</code>, and not all 419 reflection <code>Values</code> have it. 420 </p> 421 422 <p> 423 The <code>CanSet</code> method of <code>Value</code> reports the 424 settability of a <code>Value</code>; in our case, 425 </p> 426 427 {{code "/doc/progs/interface2.go" `/START f5/` `/STOP/`}} 428 429 <p> 430 prints 431 </p> 432 433 <pre> 434 settability of v: false 435 </pre> 436 437 <p> 438 It is an error to call a <code>Set</code> method on an non-settable 439 <code>Value</code>. But what is settability? 440 </p> 441 442 <p> 443 Settability is a bit like addressability, but stricter. It's the 444 property that a reflection object can modify the actual storage 445 that was used to create the reflection object. Settability is 446 determined by whether the reflection object holds the original 447 item. When we say 448 </p> 449 450 {{code "/doc/progs/interface2.go" `/START f6/` `/STOP/`}} 451 452 <p> 453 we pass a <em>copy</em> of <code>x</code> to 454 <code>reflect.ValueOf</code>, so the interface value created as the 455 argument to <code>reflect.ValueOf</code> is a <em>copy</em> of 456 <code>x</code>, not <code>x</code> itself. Thus, if the 457 statement 458 </p> 459 460 {{code "/doc/progs/interface2.go" `/START f6b/` `/STOP/`}} 461 462 <p> 463 were allowed to succeed, it would not update <code>x</code>, even 464 though <code>v</code> looks like it was created from 465 <code>x</code>. Instead, it would update the copy of <code>x</code> 466 stored inside the reflection value and <code>x</code> itself would 467 be unaffected. That would be confusing and useless, so it is 468 illegal, and settability is the property used to avoid this 469 issue. 470 </p> 471 472 <p> 473 If this seems bizarre, it's not. It's actually a familiar situation 474 in unusual garb. Think of passing <code>x</code> to a 475 function: 476 </p> 477 478 <pre> 479 f(x) 480 </pre> 481 482 <p> 483 We would not expect <code>f</code> to be able to modify 484 <code>x</code> because we passed a copy of <code>x</code>'s value, 485 not <code>x</code> itself. If we want <code>f</code> to modify 486 <code>x</code> directly we must pass our function the address of 487 <code>x</code> (that is, a pointer to <code>x</code>):</p> 488 489 <p> 490 <code>f(&x)</code> 491 </p> 492 493 <p> 494 This is straightforward and familiar, and reflection works the same 495 way. If we want to modify <code>x</code> by reflection, we must 496 give the reflection library a pointer to the value we want to 497 modify. 498 </p> 499 500 <p> 501 Let's do that. First we initialize <code>x</code> as usual 502 and then create a reflection value that points to it, called 503 <code>p</code>. 504 </p> 505 506 {{code "/doc/progs/interface2.go" `/START f7/` `/STOP/`}} 507 508 <p> 509 The output so far is 510 </p> 511 512 <pre> 513 type of p: *float64 514 settability of p: false 515 </pre> 516 517 <p> 518 The reflection object <code>p</code> isn't settable, but it's not 519 <code>p</code> we want to set, it's (in effect) <code>*p</code>. To 520 get to what <code>p</code> points to, we call the <code>Elem</code> 521 method of <code>Value</code>, which indirects through the pointer, 522 and save the result in a reflection <code>Value</code> called 523 <code>v</code>: 524 </p> 525 526 {{code "/doc/progs/interface2.go" `/START f7b/` `/STOP/`}} 527 528 <p> 529 Now <code>v</code> is a settable reflection object, as the output 530 demonstrates, 531 </p> 532 533 <pre> 534 settability of v: true 535 </pre> 536 537 <p> 538 and since it represents <code>x</code>, we are finally able to use 539 <code>v.SetFloat</code> to modify the value of 540 <code>x</code>: 541 </p> 542 543 {{code "/doc/progs/interface2.go" `/START f7c/` `/STOP/`}} 544 545 <p> 546 The output, as expected, is 547 </p> 548 549 <pre> 550 7.1 551 7.1 552 </pre> 553 554 <p> 555 Reflection can be hard to understand but it's doing exactly what 556 the language does, albeit through reflection <code>Types</code> and 557 <code>Values</code> that can disguise what's going on. Just keep in 558 mind that reflection Values need the address of something in order 559 to modify what they represent. 560 </p> 561 562 <p><b>Structs</b></p> 563 564 <p> 565 In our previous example <code>v</code> wasn't a pointer itself, it 566 was just derived from one. A common way for this situation to arise 567 is when using reflection to modify the fields of a structure. As 568 long as we have the address of the structure, we can modify its 569 fields. 570 </p> 571 572 <p> 573 Here's a simple example that analyzes a struct value, <code>t</code>. We create 574 the reflection object with the address of the struct because we'll want to 575 modify it later. Then we set <code>typeOfT</code> to its type and iterate over 576 the fields using straightforward method calls 577 (see <a href="/pkg/reflect/">package reflect</a> for details). 578 Note that we extract the names of the fields from the struct type, but the 579 fields themselves are regular <code>reflect.Value</code> objects. 580 </p> 581 582 {{code "/doc/progs/interface2.go" `/START f8/` `/STOP/`}} 583 584 <p> 585 The output of this program is 586 </p> 587 588 <pre> 589 0: A int = 23 590 1: B string = skidoo 591 </pre> 592 593 <p> 594 There's one more point about settability introduced in 595 passing here: the field names of <code>T</code> are upper case 596 (exported) because only exported fields of a struct are 597 settable. 598 </p> 599 600 <p> 601 Because <code>s</code> contains a settable reflection object, we 602 can modify the fields of the structure. 603 </p> 604 605 {{code "/doc/progs/interface2.go" `/START f8b/` `/STOP/`}} 606 607 <p> 608 And here's the result: 609 </p> 610 611 <pre> 612 t is now {77 Sunset Strip} 613 </pre> 614 615 <p> 616 If we modified the program so that <code>s</code> was created from 617 <code>t</code>, not <code>&t</code>, the calls to 618 <code>SetInt</code> and <code>SetString</code> would fail as the 619 fields of <code>t</code> would not be settable. 620 </p> 621 622 <p><b>Conclusion</b></p> 623 624 <p> 625 Here again are the laws of reflection: 626 </p> 627 628 <ol> 629 <li>Reflection goes from interface value to reflection 630 object.</li> 631 <li>Reflection goes from reflection object to interface 632 value.</li> 633 <li>To modify a reflection object, the value must be settable.</li> 634 </ol> 635 636 <p> 637 Once you understand these laws reflection in Go becomes much easier 638 to use, although it remains subtle. It's a powerful tool that 639 should be used with care and avoided unless strictly 640 necessary. 641 </p> 642 643 <p> 644 There's plenty more to reflection that we haven't covered — 645 sending and receiving on channels, allocating memory, using slices 646 and maps, calling methods and functions — but this post is 647 long enough. We'll cover some of those topics in a later 648 article. 649 </p>