12-reflection/

directory
v0.0.0-...-811d8d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2015 License: Apache-2.0

README

Reflection

Reflection is the ability to inspect a value to derive type or other meta-data. Reflection can give our program incredible flexibility to work with data of different types or create values on the fly. Reflection is critical for the encoding and decoding of data.

Notes

  • The reflection package allows us to inspect our types.
  • We can add "tags" to our struct fields to store and use meta-data.
  • Encoding package leverages reflection and we can as well.

http://blog.golang.org/laws-of-reflection

Code Review

Empty Interface (Go Playground)

Reflect struct types with tags (Go Playground)

Decoding function for integers (Go Playground)

Exercises

Exercise 1

Declare a struct type that represents a request for a customer invoice. Include a CustomerID and InvoiceID field. Define tags that can be used to validate the request. Define tags that specify both the length and range for the ID to be valid. Declare a function named validate that accepts values of any type and processes the tags. Display the results of the validation.

Template (Go Playground) | Answer (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how the empty interface works.
Sample program to show how the empty interface works.
Sample program to show how to reflect on a struct type with tags.
Sample program to show how to reflect on a struct type with tags.
Sample program to show how to use reflection to decode an integer.
Sample program to show how to use reflection to decode an integer.
exercises
exercise1
Declare a struct type that represents a request for a customer invoice.
Declare a struct type that represents a request for a customer invoice.
template1
Declare a struct type that represents a request for a customer invoice.
Declare a struct type that represents a request for a customer invoice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL