This is just how type assertion works.
If you don't use the dual return it panics if the actual type is different from the one you try to assert.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
This is just how type assertion works.
If you don't use the dual return it panics if the actual type is different from the one you try to assert.
x
and its type is T
. If the type assertion is false,
a run-time panic occurs."To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
The reason is directly stated in the Go language spec:
"If the type assertion holds, the value of the expression is the value stored inx
and its type isT
. If the type assertion is false, a run-time panic occurs."Here "hold" means if it succeeds.
I am not sure but perhaps as simple as it is a very natural and known behavior of maps and to make it work syntactically as the type assertion would make it weird.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
I am not sure but perhaps as simple as it is a very natural and known behavior of maps and to make it work syntactically as the type assertion would make it weird.
* A failed type assertion always panic'd
* A failed type assertion never panic'd and instead returned the zero value, as the map lookup does
* Something else
Dave
I know of the syntax in spec.
I just want to understand what is the deep reason for the syntax inconsistency between map index and type assert.
TL, the simplest reason is this:For compiler's perspective, see Andrey's response.
You can live without the "ok idiom" when retrieving a map value but you cannot when type asserting. Think of the consequences for rest of your program, if you forgot to check the status of assertion that is failed (but you didn't know) because the compiler didn't panic! It would be a travesty.
This is why go requires you to work hard for this behaviour by making the single value form of the expression the panicing default and the two value form harder to use accidentally.
The same logic I described also applies to map lookup.
Sorry, I misspoke, this logic does not apply to map lookup, they are unrelated other than having a one arg and two arg form.
If you think that's inconsistent, you should see how we used to delete things from maps ;)
Sorry, I misspoke, this logic does not apply to map lookup
A type assertion is dangerous because if the thing you are asserting to doesn't match the type of the value inside the interface, what type should the result be? With the map example the type of the result is known.
That's what happens now with the two arg version, the one arg version panics rather than letting you think you got a valid value of the type you expected.
That's what happens now with the two arg version, the one arg version panics rather than letting you think you got a valid value of the type you expected.
Playground link: https://play.golang.org/p/oS7SWHXgJQ
not in similarities of syntax.I think you still can't "get" the chief distinction: it lies in typing
For
m := make(map[string]interface{})
there's a well-defined zero value for the type of values this map holds.
While somewhat dubious, this is useful for maps which merely record the
fact the particular set of keys is "known"; consider:
m := make(map[string]bool)
m["three"] = true
And now:
* m["one"] evaluates to false
* m["two"] evaluates to false
* m["three"] evaluates to true
* m["four"] evaluates to false
...and so on.
With type assertions, I really fail to see how returning the zero value
for the target type in case the type assertion failed can be useful.
I don't think it is based on the zero value being useful but a desire to be as exact as possible when working with unknown data. You reach for X when you have Y. Why would you magically get an X back? Panic can arguably be seen as too strong but the multiple return is awesome.
--