Grokking Advanced Go
0% completed
Type Assertions and Type Switches
A function receives a value of type any or io.Writer. The static type gives little information about the stored value, but the first word of the interface value records its dynamic type. How can code learn what the value holds, and what happens when its guess is wrong?
Go offers two forms for this question, and both read the same type word. A type assertion tests for one type. A type switch tests for several types in order. This page covers both forms, the exact panic of a failed assertion, and the point at which a method would serve better than either.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%