go Are there errors in canonical type assertions?
A type assertion used in an assignment statement or initialization of the special form v, ok = x.(T) v, ok := x.(T) var v, ok = x.(T) var v, ok interface{} = x.(T) // dynamic types of v and ok are T and bool yields an additional untyped boolean value.
What is the meaning of the last example?var v, good interface {} = x.(t)
?
I'm getting an error in go 1.19syntax Error: Unexpected interface, expecting := or = or comma
All these lines are trying the same thing: Type assertion forx
to typeT
. The valueok
determines whether the assertion was successful. In the last example you provided, the only difference is that instead of determining the types forv
andok
, you providedinterface{}## for both # type. Declaring
vand
okas
interface{}does not change the values they contain. It allows you to send them to a function or add them to a collection that requires an
interface{}type, at which point they must be asserted again.
The above is the detailed content of Go: Type assertions - is there a bug in the specification?. For more information, please follow other related articles on the PHP Chinese website!