Type Assertion

This lesson explains converting the type of a given value to another specific value using examples

We'll cover the following

Introduction

In the previous lesson, we covered type conversions. Now, we will move on to discuss how, if you have a value that you want to convert to another or a specific type (in case of interface{}), you can use type assertion. A type assertion takes a value and tries to create another version in the specified explicit type.

Examples

In the example below, the timeMap function takes a value and if it can be asserted as a map of interfaces{} keyed by strings, then it injects a new entry called “updated_at” with the current time value.

Get hands-on with 1400+ tech skills courses.