reflection/

directory
v0.0.0-...-6749d27 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2016 License: Apache-2.0

README

Reflection - Standard Library

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

Basics

Example shows how to reflect over a struct type value.
Struct Types (Go Playground)

Example shows how to reflect over a slice of struct type values.
Slices (Go Playground)

Example shows how to reflect over a map of struct type values.
Maps (Go Playground)

Example shows how to reflect over a struct type pointer.
Pointers (Go Playground)

Interfaces

Example shows how to reflect over a struct type value that is stored inside an interface value.
Struct Types (Go Playground)

Example shows how to reflect over a slice of struct type values that are stored inside an interface value.
Slices (Go Playground)

Example shows how to reflect over a map of struct type values that are stored inside an interface value.
Maps (Go Playground)

Example shows how to reflect over a struct type pointer that is stored inside an interface value.
Pointers (Go Playground)

Tags

Example shows how to reflect on a struct type with tags.
Tags (Go Playground)

Inspection / Decoding

Example shows how to inspect a structs fields and display the field name, type and value.
Struct Types (Go Playground)

Example shows how to use reflection to decode an integer.
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)


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

Directories

Path Synopsis
basics
map
Example shows how to reflect over a map of struct type values.
Example shows how to reflect over a map of struct type values.
pointer
Example shows how to reflect over a struct type pointer.
Example shows how to reflect over a struct type pointer.
slice
Example shows how to reflect over a slice of struct type values.
Example shows how to reflect over a slice of struct type values.
struct
Example shows how to reflect over a struct type value.
Example shows how to reflect over a struct type value.
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.
inspect
integer
Example shows how to use reflection to decode an integer.
Example shows how to use reflection to decode an integer.
struct
Example shows how to inspect a structs fields and display the field name, type and value.
Example shows how to inspect a structs fields and display the field name, type and value.
interface
map
Example shows how to reflect over a map of struct type values that are stored inside an interface value.
Example shows how to reflect over a map of struct type values that are stored inside an interface value.
pointer
Example shows how to reflect over a struct type pointer that is stored inside an interface value.
Example shows how to reflect over a struct type pointer that is stored inside an interface value.
slice
Example shows how to reflect over a slice of struct type values that are stored inside an interface value.
Example shows how to reflect over a slice of struct type values that are stored inside an interface value.
struct
Example shows how to reflect over a struct type value that is stored inside an interface value.
Example shows how to reflect over a struct type value that is stored inside an interface value.
Example shows how to reflect on a struct type with tags.
Example shows how to reflect on a struct type with tags.

Jump to

Keyboard shortcuts

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