Go is statically typed, so values of unknown type only exist inside interfaces. To work with them you use type assertions and type switches, which inspect the dynamic type at run time. Type conversions are different: they change a value of a known type into another known type and never apply to the hidden type inside an interface.
This article shows how to handle dynamic values safely, how type switches and errors.As fit in, and where conversions come in after you have recovered the concrete type.
An interface value stores a pair: the dynamic type and the dynamic value. Dynamic types appear when you decode JSON into any, accept any parameters, receive errors, or store mixed values in a []any.
JSON numbers decode to float64, which surprises many people. You have to assert to float64 and then convert to int if you need an integer.
The assertion v, ok := x.(T) is the safe way to test one type.
The single-value form x.(T) panics when the dynamic type is not T, so use it only when failure means a bug in your program. Note the two steps above: the assertion recovers float64, and the conversion int(age) then changes it to an integer.
A type switch tests many types in one statement and gives you a correctly typed variable in each branch.
Order matters: the first matching case wins, so put concrete types before interface types like fmt.Stringer. In a case that lists several types, the variable keeps the interface type of the switch expression.
| Question | Type assertion | Type conversion |
|---|---|---|
| Works on | Interface values | Concrete values |
| Looks at | The dynamic type | The static type |
| Syntax | x.(T) or v.(type) | T(x) |
| Fails by | Panic or ok == false | Compile error |
| Typical use | Decode results, optional interfaces, errors | Numeric, string and byte conversions |
You cannot write int(x) when x is any; the compiler reports that an interface cannot be converted that way. Assert first, convert second.
A nil interface has no dynamic type, so every non-nil case fails and only case nil matches. A subtle trap is an interface holding a nil pointer: the dynamic type is *T, the value is nil, and the interface itself is not nil. Comparing such an interface with nil returns false, which is the cause of many "error is not nil" bugs when a function returns a typed nil pointer as error. Return a literal nil instead.
any. The decoder then does the type checking and gives you real field types.errors.Is and errors.As for errors rather than asserting on err, so wrapped errors still match.func Map[T, U any](s []T, f func(T) U) []U.No. Only a type assertion or type switch can extract the concrete type. A conversion between interface types is allowed only when the compiler can prove it is valid.
When decoding into any, encoding/json represents every number as float64. Decode into a typed struct, or use Decoder.UseNumber, to avoid it.
The difference is small. Choose by readability: use a type switch when you test more than one type.
interface{} versus any change?Nothing at run time. any is an alias for interface{} introduced in Go 1.18.
Use type assertions and type switches to recover the concrete type of an interface value at run time, preferring the comma-ok form. Use conversions to change a known concrete type into another. For decoded data and errors, typed structs, errors.As and generics usually remove the need for most assertions.