2017-03-31 18:01:58 +02:00

1.2 KiB

Reflection Functions

Sprig provides rudimentary reflection tools. These help advanced template developers understand the underlying Go type information for a particular value.

Go has several primitive kinds, like string, slice, int64, and bool.

Go has an open type system that allows developers to create their own types.

Sprig provides a set of functions for each.

Kind Functions

There are two Kind functions: kindOf returns the kind of an object.

kindOf "hello"

The above would return string. For simple tests (like in if blocks), the isKind function will let you verify that a value is a particular kind:

kindIs "int" 123

The above will return true

Type Functions

Types are slightly harder to work with, so there are three different functions:

  • typeOf returns the underlying type of a value: typeOf $foo
  • typeIs is like kindIs, but for types: typeIs "*io.Buffer" $myVal
  • typeIsLike works as kindIs, except that it also dereferences pointers.

Note: None of these can test whether or not something implements a given interface, since doing so would require compiling the interface in ahead of time.