3 ms·
> If you incorrectly cast an interface{} to a type it's not, it will be a runtime error. Sidenote, you may or may not have meant a panic - so i just want to cl
by fizzbatter 11y ago
> If you incorrectly cast an interface{} to a type it's not, it will be a runtime error.
Sidenote, you may or may not have meant a panic - so i just want to clarify.
Casting an interface to a type incorrectly will not cause a panic/crash, assuming you use the proper syntax.
// Will panic
foo := myInterface.(BadType)
// Will not panic
foo, ok := myInterface.(BadType)
In the latter, you simply check if `ok` is true or not. If not, the interface value does not implement the given type/interface.