| 1 |
# Reflection Functions |
| 2 |
|
| 3 |
Sprig provides rudimentary reflection tools. These help advanced template |
| 4 |
developers understand the underlying Go type information for a particular value. |
| 5 |
|
| 6 |
Go has several primitive _kinds_, like `string`, `slice`, `int64`, and `bool`. |
| 7 |
|
| 8 |
Go has an open _type_ system that allows developers to create their own types. |
| 9 |
|
| 10 |
Sprig provides a set of functions for each. |
| 11 |
|
| 12 |
## Kind Functions |
| 13 |
|
| 14 |
There are two Kind functions: `kindOf` returns the kind of an object. |
| 15 |
|
| 16 |
``` |
| 17 |
kindOf "hello" |
| 18 |
``` |
| 19 |
|
| 20 |
The above would return `string`. For simple tests (like in `if` blocks), the |
| 21 |
`kindIs` function will let you verify that a value is a particular kind: |
| 22 |
|
| 23 |
``` |
| 24 |
kindIs "int" 123 |
| 25 |
``` |
| 26 |
|
| 27 |
The above will return `true` |
| 28 |
|
| 29 |
## Type Functions |
| 30 |
|
| 31 |
Types are slightly harder to work with, so there are three different functions: |
| 32 |
|
| 33 |
- `typeOf` returns the underlying type of a value: `typeOf $foo` |
| 34 |
- `typeIs` is like `kindIs`, but for types: `typeIs "*io.Buffer" $myVal` |
| 35 |
- `typeIsLike` works as `typeIs`, except that it also dereferences pointers. |
| 36 |
|
| 37 |
**Note:** None of these can test whether or not something implements a given |
| 38 |
interface, since doing so would require compiling the interface in ahead of time. |
| 39 |
|
| 40 |
## deepEqual |
| 41 |
|
| 42 |
`deepEqual` returns true if two values are ["deeply equal"](https://golang.org/pkg/reflect/#DeepEqual) |
| 43 |
|
| 44 |
Works for non-primitive types as well (compared to the built-in `eq`). |
| 45 |
|
| 46 |
``` |
| 47 |
deepEqual (list 1 2 3) (list 1 2 3) |
| 48 |
``` |
| 49 |
|
| 50 |
The above will return `true` |